json
sealed class jsonAvailable 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;
}
Members
FromStringmethod
public static fn FromString(string text) : json throws JsonParseErrorParses 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.
FrozenFromStringmethod
public static fn FrozenFromString(string text) : jsonParses and recursively freezes a value. Invalid text panics rather than throwing; use FromString for recoverable parse errors.
ArrayFromStringmethod
public static fn ArrayFromString(string text) : json[] throws JsonParseErrorParses 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.
ArrayToStringmethod
public static fn ArrayToString(json[] values) : stringSerializes the supplied elements as one compact JSON array. Nonfinite numbers become null.
ToStringmethod
public fn ToString() : stringSerializes compact strict JSON. NaN and infinities become null. Nesting deeper than MaxWriteDepth, including cycles, panics.
ToStringBuffermethod
public fn ToStringBuffer(StringBuilder builder) : int32Appends compact strict JSON and returns the number of UTF-8 bytes appended. A depth-limit panic may leave a partial result in the builder.
MaxWriteDepthconst
const int32 MaxWriteDepth = 512The maximum nested depth accepted by the JSON writers, with the root at depth zero.
TypeOfmethod
public fn TypeOf() : JsonTypeReturns the JSON category, collapsing all numeric representations into Number.
HasPropertymethod
public fn HasProperty(string key) : boolTests whether an object contains the key, even if its value is null. Non-objects return false; lookup is linear in property count.
GetPropertyTypemethod
public fn GetPropertyType(string key) : JsonTypeReturns the property category, or Null for an absent key or non-object. A present null property has the same result.
GetPropertymethod
public fn GetProperty(string key) : jsonReturns a property value, or JSON null for a missing key or non-object. Returned containers share the original document's nodes.
GetElementmethod
public fn GetElement(int32 index) : jsonReturns an array element, or JSON null for an invalid index or non-array. Returned containers share the original nodes.
Lengthmethod
public fn Length() : int32Returns the array element count, or zero for other kinds.
PropertyCountmethod
public fn PropertyCount() : int32Returns the object property count, or zero for other kinds.
DeepEqualsmethod
public fn DeepEquals(json other) : boolRecursively 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.
GetHashCodemethod
public fn GetHashCode() : uint32Hashes 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.
Equalsmethod
public fn Equals(object other) : boolUses DeepEquals when other is a JSON value, otherwise returns false.
DeletePropertymethod
public fn DeleteProperty(string key) : voidRemoves an object property in place. Missing keys and non-objects do nothing, but a frozen object panics even if the key is absent.
SetPropertymethod
public fn SetProperty(string key, json value) : voidInserts or replaces an object property in place, retaining the supplied node. Non-objects do nothing; frozen objects panic.
GetKeysmethod
public fn GetKeys() : string[]Returns a new array of object keys in stored order, or an empty array for other kinds.
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.
Pushmethod
public fn Push(json value) : voidAppends a shared node to an array. Non-arrays do nothing; frozen arrays panic.
Popmethod
public fn Pop() : jsonRemoves and returns the final array element, or null for an empty array or non-array. A frozen array panics even when empty.
RemoveAtmethod
public fn RemoveAt(int32 index) : voidRemoves an array element and shifts later entries. Invalid indices and non-arrays do nothing; a frozen array panics before index validation.
SetElementmethod
public fn SetElement(int32 index, json value) : voidSets 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.
Clonemethod
public fn Clone() : jsonRecursively copies an acyclic document into mutable containers. Immutable strings may remain shared. Does not preserve shared-subtree identity and does not support cycles.
ToFormattedStringmethod
public fn ToFormattedString() : stringSerializes 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.
IsFrozenmethod
public fn IsFrozen() : boolTests the frozen flag on an object or array. Scalars and null return false.
Freezemethod
public fn Freeze() : jsonRecursively 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.
Kindmethod
public fn Kind() : JsonKindReturns the representation kind, distinguishing int32, int64, float, and double. Safe for JSON null.
constructorconstructor
public constructor ()Provided by the compiler.