zena:json

zena
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 ​

zena
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

zena
final class SourceLocation
Constructors
zena
new(line: i32, column: i32, endLine: i32, endColumn: i32)
#
Properties
zena
line: i32
#
zena
column: i32
#
zena
endLine: i32
#
zena
endColumn: i32
#

JsonObject

zena
class JsonObject
Constructors
zena
new(withLocations: boolean = false)
#
Properties
zena
size: i32 { get; }
#
Methods
zena
static withLocations(): JsonObject
#
zena
has(key: String): boolean
#
zena
delete(key: String): boolean
#
zena
locationOf(key: String): SourceLocation | null
#
zena
setLocation(key: String, loc: SourceLocation): void
#
zena
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.

zena
stringAt(path: String): inline (true, String) | inline (false, _)
#

The string at path (see lookup), found only when it is there and is a string.

zena
let uri = message.stringAt('params.textDocument.uri') ?? '';
zena
numberAt(path: String): inline (true, f64) | inline (false, _)
#

The number at path, found only when it is a number.

zena
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.

zena
boolAt(path: String): inline (true, boolean) | inline (false, _)
#

The boolean at path, found only when it is a boolean.

zena
objectAt(path: String): inline (true, JsonObject) | inline (false, _)
#

The object at path, found only when it is an object.

zena
arrayAt(path: String): inline (true, JsonArray) | inline (false, _)
#

The array at path, found only when it is an array.

zena
forEach(callback: (key: String, value: JsonValue) => void): void
#
zena
keys(): Iterator<String>
#

The keys, in the order the fields were added.

zena
stringify(): String
#
Operators
zena
operator [](key: String): JsonValue
#
zena
operator []=(key: String, value: JsonValue): void
#

JsonArray

zena
class JsonArray
Constructors
zena
new(withLocations: boolean = false)
#
Properties
zena
length: i32 { get; }
#
Methods
zena
static withLocations(): JsonArray
#
zena
push(value: JsonValue): void
#
zena
pushWithLocation(value: JsonValue, loc: SourceLocation): void
#
zena
locationOf(index: i32): SourceLocation | null
#
zena
stringify(): String
#
Operators
zena
operator [](index: i32): JsonValue
#
zena
operator []=(index: i32, value: JsonValue): void
#

JsonParseError

zena
class JsonParseError extends Error
Extends Error
Constructors
zena
new(message: String, line: i32, column: i32)
#
Properties
zena
line: i32 { get; }
#
zena
column: i32 { get; }
#
2 inherited members
From Error
Properties
zena
message: String
#
Methods
zena
getStackTrace(): String | null
#

JsonBuilder

zena
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"]}

Constructors
zena
new(capacity: i32 = 64)
#
Methods
zena
beginObject(): JsonBuilder
#
zena
endObject(): JsonBuilder
#
zena
beginArray(): JsonBuilder
#
zena
endArray(): JsonBuilder
#
zena
key(k: String): JsonBuilder
#
zena
string(v: String): JsonBuilder
#
zena
i32(v: i32): JsonBuilder
#
zena
i64(v: i64): JsonBuilder
#
zena
f64(v: f64): JsonBuilder
#
zena
bool(v: boolean): JsonBuilder
#
zena
nullValue(): JsonBuilder
#
zena
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.

zena
raw(json: String): JsonBuilder
#
zena
toString(): String
#
zena
clear(): void
#

Type aliases

JsonValue

zena
type JsonValue = JsonObject | JsonArray | String | Box<f64> | Box<boolean> | null

JsonOptions

zena
type JsonOptions = {allowComments: boolean, trackLocations: boolean}

Functions

stringify

zena
function stringify(value: JsonValue, indent: String | null = null): String

Serialize any JSON value to a string.

value

The JSON value to serialize

indent

Optional indentation string for pretty-printing (e.g., " " or "\t")

parseJson

zena
function parseJson(input: String, options: JsonOptions | null = null): JsonValue

jsonGet

zena
function jsonGet(value: JsonValue, path: String): JsonValue