zena:json
import {…} from 'zena:json';
JSON serialization, deserialization, and AST representation.
Provides functions to parse JSON text into structured JsonValue trees and
format values back into JSON strings.
Examples ​
import { parseJson, stringify, JsonObject } from 'zena:json';
let data = parseJson('{"name": "Zena", "version": 1}');
if (data is JsonObject) {
let obj = data as JsonObject;
let name = obj['name'];
}
let text = stringify(data);
Classes
SourceLocation
final class SourceLocation
JsonObject
class JsonObject
new(withLocations: boolean = false)
size: i32 { get; }
static withLocations(): JsonObject
has(key: String): boolean
delete(key: String): boolean
locationOf(key: String): SourceLocation | null
setLocation(key: String, loc: SourceLocation): void
lookup(path: String): JsonValue
The value at a path of keys separated by dots, like
'params.textDocument.uri'. Null when a key is missing, or when a
value on the way is not an object.
stringAt(path: String): inline (true, String) | inline (false, _)
The string at path (see lookup), found only when it is there and
is a string.
let uri = message.stringAt('params.textDocument.uri') ?? '';
numberAt(path: String): inline (true, f64) | inline (false, _)
The number at path, found only when it is a number.
intAt(path: String): inline (true, i32) | inline (false, _)
The integer at path, found only when it is a number with no
fractional part that fits an i32.
boolAt(path: String): inline (true, boolean) | inline (false, _)
The boolean at path, found only when it is a boolean.
objectAt(path: String): inline (true, JsonObject) | inline (false, _)
The object at path, found only when it is an object.
arrayAt(path: String): inline (true, JsonArray) | inline (false, _)
The array at path, found only when it is an array.
forEach(callback: (key: String, value: JsonValue) => void): void
keys(): Iterator<String>
The keys, in the order the fields were added.
stringify(): String
JsonArray
class JsonArray
JsonParseError
class JsonParseError extends Error
new(message: String, line: i32, column: i32)
JsonBuilder
class JsonBuilder
JsonBuilder - Streaming JSON construction
Provides a fluent API for building JSON strings incrementally.
Example: let json = new JsonBuilder() .beginObject() .key("name").string("example") .key("count").i32(42) .key("items").beginArray() .string("a") .string("b") .endArray() .endObject() .toString(); // {"name":"example","count":42,"items":["a","b"]}
new(capacity: i32 = 64)
beginObject(): JsonBuilder
endObject(): JsonBuilder
beginArray(): JsonBuilder
endArray(): JsonBuilder
key(k: String): JsonBuilder
string(v: String): JsonBuilder
i32(v: i32): JsonBuilder
i64(v: i64): JsonBuilder
f64(v: f64): JsonBuilder
bool(v: boolean): JsonBuilder
nullValue(): JsonBuilder
value(v: JsonValue): JsonBuilder
Writes a parsed value as it came: a request's id echoed into its
reply, or a whole object passed through. A number with no fractional
part is written as an integer, so 7 stays 7.
raw(json: String): JsonBuilder
toString(): String
clear(): void
Type aliases
JsonValue
type JsonValue = JsonObject | JsonArray | String | Box<f64> | Box<boolean> | null
JsonOptions
type JsonOptions = {allowComments: boolean, trackLocations: boolean}
Functions
stringify
function stringify(value: JsonValue, indent: String | null = null): String
Serialize any JSON value to a string.
parseJson
function parseJson(input: String, options: JsonOptions | null = null): JsonValue
jsonGet
function jsonGet(value: JsonValue, path: String): JsonValue