Skip to content

Json

Overview

The Json type provides handle-based JSON parsing, building, and serialization. All methods are static. Parsed or built JSON trees are referenced by integer handles.

Parsing

int h = Json.Parse("{\"name\": \"Alice\", \"age\": 30}");
string name = Json.GetString(Json.Get(h, "name"));
int age = Json.GetInt(Json.Get(h, "age"));
Json.Free(h);

Parse returns a handle (int). Returns 0 on parse error.

Type Checks

int h = Json.Parse(data);
Json.IsObject(h) // 1 if object
Json.IsArray(h) // 1 if array
Json.IsString(h) // 1 if string
Json.IsInt(h) // 1 if integer
Json.IsFloat(h) // 1 if float
Json.IsNumber(h) // 1 if int or float
Json.IsBool(h) // 1 if boolean
Json.IsNull(h) // 1 if null or invalid handle

Value Getters

string s = Json.GetString(h); // "" if not a string
int n = Json.GetInt(h); // 0 if not numeric
float f = Json.GetFloat(h); // 0.0 if not numeric
int b = Json.GetBool(h); // 0 if not bool

Object Access

int h = Json.Parse("{\"x\": 10, \"y\": 20}");
int child = Json.Get(h, "x"); // Get field by key (0 if missing)
int count = Json.Count(h); // Number of fields
string key = Json.KeyAt(h, 0); // Key at index (for iteration)
int val = Json.ValueAt(h, 0); // Value handle at index

Array Access

int h = Json.Parse("[10, 20, 30]");
int elem = Json.GetAt(h, 1); // Element at index (0 if out of bounds)
int len = Json.Count(h); // Number of elements

Building JSON

Create JSON structures programmatically:

int obj = Json.NewObject();
Json.SetField(obj, "name", Json.FromString("Alice"));
Json.SetField(obj, "age", Json.FromInt(30));
Json.SetField(obj, "active", Json.FromBool(1));
int arr = Json.NewArray();
Json.AddElement(arr, Json.FromInt(1));
Json.AddElement(arr, Json.FromInt(2));
Json.SetField(obj, "scores", arr);
int nullVal = Json.Null(); // JSON null value

Serialization

string json = Json.Stringify(h); // Compact JSON string

Cleanup

Json.Free(h); // Free a parsed/built JSON tree

Handles from Parse, NewObject, NewArray, and From* should be freed when no longer needed. Freeing a parent frees all children.

Iteration

Iterate over object fields or array elements using Count, KeyAt/ValueAt, and GetAt:

// Object iteration
int h = Json.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}");
int i = 0;
while (i < Json.Count(h)) {
string key = Json.KeyAt(h, i);
int val = Json.GetInt(Json.ValueAt(h, i));
i = i + 1;
}
// Array iteration
int arr = Json.Parse("[10, 20, 30]");
int sum = 0;
int j = 0;
while (j < Json.Count(arr)) {
sum = sum + Json.GetInt(Json.GetAt(arr, j));
j = j + 1;
}

Methods Reference

MethodSignatureDescription
Parse(string json) -> intParse JSON string, returns handle (0 on error)
Stringify(int handle) -> stringSerialize to compact JSON string
Free(int handle)Free handle and all children
IsNull(int h) -> int1 if null or invalid
IsBool(int h) -> int1 if boolean
IsInt(int h) -> int1 if integer
IsFloat(int h) -> int1 if float
IsNumber(int h) -> int1 if int or float
IsString(int h) -> int1 if string
IsArray(int h) -> int1 if array
IsObject(int h) -> int1 if object
GetString(int h) -> stringGet string value
GetInt(int h) -> intGet integer value
GetFloat(int h) -> floatGet float value
GetBool(int h) -> intGet boolean (0/1)
Get(int h, string key) -> intGet object field by key
GetAt(int h, int index) -> intGet array element by index
Count(int h) -> intArray length or object field count
KeyAt(int h, int index) -> stringObject key at index
ValueAt(int h, int index) -> intObject value handle at index
NewObject() -> intCreate empty object
NewArray() -> intCreate empty array
SetField(int h, string key, int val)Set object field
AddElement(int h, int val)Append to array
FromString(string val) -> intCreate string node
FromInt(int val) -> intCreate integer node
FromFloat(float val) -> intCreate float node
FromBool(int val) -> intCreate boolean node
Null() -> intCreate null node