← Standard library

json

sealed class json

Available without an import.

A dynamic JSON value: null, Boolean, number, string, object, or array. Objects and arrays are shared mutable references until recursively frozen. Parsing accepts JSON5 conveniences; serialization writes strict JSON and replaces nonfinite numbers with null. Use Clone to obtain a mutable deep copy of an acyclic document.

api fn Main() : int32 {
    json original = { count: 2 };
    original.Freeze();
    json copy = original.Clone();
    copy.SetProperty("count", 3);
    return original.ToString() == "{\"count\":2}" && copy.ToString() == "{\"count\":3}" ? 1 : 0;
}

Json.wax:24

Members

FromStringmethod

public static fn FromString(string text) : json throws JsonParseError

Parses one complete JSON5-style value, allowing comments, single-quoted strings, unquoted keys, trailing commas, hexadecimal integers, and Infinity/NaN. Throws JsonParseError for malformed input or trailing non-whitespace content. The result owns its decoded data.

Json.wax:38

FrozenFromStringmethod

public static fn FrozenFromString(string text) : json

Parses and recursively freezes a value. Invalid text panics rather than throwing; use FromString for recoverable parse errors.

Json.wax:51

ArrayFromStringmethod

public static fn ArrayFromString(string text) : json[] throws JsonParseError

Parses a top-level array and returns a new array of its elements. Throws JsonParseError for invalid text or a non-array root. Element nodes are not deep-copied.

Json.wax:60

ArrayToStringmethod

public static fn ArrayToString(json[] values) : string

Serializes the supplied elements as one compact JSON array. Nonfinite numbers become null.

Json.wax:70

ToStringmethod

public fn ToString() : string

Serializes compact strict JSON. NaN and infinities become null. Nesting deeper than MaxWriteDepth, including cycles, panics.

Json.wax:81

ToStringBuffermethod

public fn ToStringBuffer(StringBuilder builder) : int32

Appends compact strict JSON and returns the number of UTF-8 bytes appended. A depth-limit panic may leave a partial result in the builder.

Json.wax:90

MaxWriteDepthconst

const int32 MaxWriteDepth = 512

The maximum nested depth accepted by the JSON writers, with the root at depth zero.

Json.wax:102

TypeOfmethod

public fn TypeOf() : JsonType

Returns the JSON category, collapsing all numeric representations into Number.

Json.wax:237

HasPropertymethod

public fn HasProperty(string key) : bool

Tests whether an object contains the key, even if its value is null. Non-objects return false; lookup is linear in property count.

Json.wax:250

GetPropertyTypemethod

public fn GetPropertyType(string key) : JsonType

Returns the property category, or Null for an absent key or non-object. A present null property has the same result.

Json.wax:257

GetPropertymethod

public fn GetProperty(string key) : json

Returns a property value, or JSON null for a missing key or non-object. Returned containers share the original document's nodes.

Json.wax:271

GetElementmethod

public fn GetElement(int32 index) : json

Returns an array element, or JSON null for an invalid index or non-array. Returned containers share the original nodes.

Json.wax:284

Lengthmethod

public fn Length() : int32

Returns the array element count, or zero for other kinds.

Json.wax:307

PropertyCountmethod

public fn PropertyCount() : int32

Returns the object property count, or zero for other kinds.

Json.wax:314

DeepEqualsmethod

public fn DeepEquals(json other) : bool

Recursively compares acyclic documents. Array order matters; object property order does not. Different numeric kinds compare after conversion to double, which can lose large-integer precision. NaN is unequal to NaN.

Json.wax:356

GetHashCodemethod

public fn GetHashCode() : uint32

Hashes document contents, ignoring object property order. Requires an acyclic document. Mutation changes the hash; do not mutate a document while using it as a hash key.

Json.wax:403

Equalsmethod

public fn Equals(object other) : bool

Uses DeepEquals when other is a JSON value, otherwise returns false.

Json.wax:431

DeletePropertymethod

public fn DeleteProperty(string key) : void

Removes an object property in place. Missing keys and non-objects do nothing, but a frozen object panics even if the key is absent.

Json.wax:445

SetPropertymethod

public fn SetProperty(string key, json value) : void

Inserts or replaces an object property in place, retaining the supplied node. Non-objects do nothing; frozen objects panic.

Json.wax:473

GetKeysmethod

public fn GetKeys() : string[]

Returns a new array of object keys in stored order, or an empty array for other kinds.

Json.wax:510

GetValuesmethod

public fn GetValues() : json[]

Returns a new array of object property values in the same order as GetKeys, or an empty array for other kinds. Child nodes remain shared.

Json.wax:522

Pushmethod

public fn Push(json value) : void

Appends a shared node to an array. Non-arrays do nothing; frozen arrays panic.

Json.wax:555

Popmethod

public fn Pop() : json

Removes and returns the final array element, or null for an empty array or non-array. A frozen array panics even when empty.

Json.wax:584

RemoveAtmethod

public fn RemoveAt(int32 index) : void

Removes an array element and shifts later entries. Invalid indices and non-arrays do nothing; a frozen array panics before index validation.

Json.wax:603

SetElementmethod

public fn SetElement(int32 index, json value) : void

Sets an array element, growing and filling any gap with JSON null. Negative indices and non-arrays do nothing; frozen arrays panic before index validation. The resulting array length must fit int32 and available memory.

Json.wax:624

Clonemethod

public fn Clone() : json

Recursively copies an acyclic document into mutable containers. Immutable strings may remain shared. Does not preserve shared-subtree identity and does not support cycles.

Json.wax:694

ToFormattedStringmethod

public fn ToFormattedString() : string

Serializes strict JSON with two-space indentation and one container entry per line. Empty containers stay compact; nonfinite numbers become null. Uses the same nesting limit as ToString.

Json.wax:735

IsFrozenmethod

public fn IsFrozen() : bool

Tests the frozen flag on an object or array. Scalars and null return false.

Json.wax:759

Freezemethod

public fn Freeze() : json

Recursively freezes this container and reachable containers in place, returning this value. Aliases observe the freeze. Scalars/null are unchanged; already-frozen nodes stop traversal, including cycles.

Json.wax:773

Kindmethod

public fn Kind() : JsonKind

Returns the representation kind, distinguishing int32, int64, float, and double. Safe for JSON null.

Json.wax:823

constructorconstructor

public constructor ()

Provided by the compiler.