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 objectJson.IsArray(h) // 1 if arrayJson.IsString(h) // 1 if stringJson.IsInt(h) // 1 if integerJson.IsFloat(h) // 1 if floatJson.IsNumber(h) // 1 if int or floatJson.IsBool(h) // 1 if booleanJson.IsNull(h) // 1 if null or invalid handleValue Getters
string s = Json.GetString(h); // "" if not a stringint n = Json.GetInt(h); // 0 if not numericfloat f = Json.GetFloat(h); // 0.0 if not numericint b = Json.GetBool(h); // 0 if not boolObject 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 fieldsstring key = Json.KeyAt(h, 0); // Key at index (for iteration)int val = Json.ValueAt(h, 0); // Value handle at indexArray 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 elementsBuilding 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 valueSerialization
string json = Json.Stringify(h); // Compact JSON stringCleanup
Json.Free(h); // Free a parsed/built JSON treeHandles 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 iterationint 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 iterationint 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
| Method | Signature | Description |
|---|---|---|
Parse | (string json) -> int | Parse JSON string, returns handle (0 on error) |
Stringify | (int handle) -> string | Serialize to compact JSON string |
Free | (int handle) | Free handle and all children |
IsNull | (int h) -> int | 1 if null or invalid |
IsBool | (int h) -> int | 1 if boolean |
IsInt | (int h) -> int | 1 if integer |
IsFloat | (int h) -> int | 1 if float |
IsNumber | (int h) -> int | 1 if int or float |
IsString | (int h) -> int | 1 if string |
IsArray | (int h) -> int | 1 if array |
IsObject | (int h) -> int | 1 if object |
GetString | (int h) -> string | Get string value |
GetInt | (int h) -> int | Get integer value |
GetFloat | (int h) -> float | Get float value |
GetBool | (int h) -> int | Get boolean (0/1) |
Get | (int h, string key) -> int | Get object field by key |
GetAt | (int h, int index) -> int | Get array element by index |
Count | (int h) -> int | Array length or object field count |
KeyAt | (int h, int index) -> string | Object key at index |
ValueAt | (int h, int index) -> int | Object value handle at index |
NewObject | () -> int | Create empty object |
NewArray | () -> int | Create empty array |
SetField | (int h, string key, int val) | Set object field |
AddElement | (int h, int val) | Append to array |
FromString | (string val) -> int | Create string node |
FromInt | (int val) -> int | Create integer node |
FromFloat | (float val) -> int | Create float node |
FromBool | (int val) -> int | Create boolean node |
Null | () -> int | Create null node |