Bundle JSON parsing and building library. JsonParser reads JSON strings into a JsonElement tree; JsonBuilder constructs JSON documents using a stack-based API. Compile with -lib json.
JsonBuilder
Stack driven JSON builder
builder := JsonBuilder->New();
money := builder->PushObject();
money->Insert("type", "usd");
money->Insert("amount", "1013");
deposits := JsonElement->New(JsonElement->JsonType->ARRAY);
deposits->Add(115);
deposits->Add(318);
deposits->Add(580);
money->Insert("deposits", deposits);
builder->Pop()->ToString()->PrintLine();Operations
Add #
Adds a value
method : public : Add(value:String) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| value | String | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Example
builder := Data.JSON.JsonBuilder->New();
arr := builder->PushArray();
arr->Add("red");
arr->Add("green");
arr->Add("blue");
builder->Pop()->ToString()->PrintLine();Add #
Adds a value
method : public : Add(value:Int) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| value | Int | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Example
builder := Data.JSON.JsonBuilder->New();
arr := builder->PushArray();
arr->Add(1);
arr->Add(2);
arr->Add(3);
builder->Pop()->ToString()->PrintLine();Add #
Adds a value
method : public : Add(value:Float) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| value | Float | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Add #
Adds a value
method : public : Add(value:Bool) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| value | Bool | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Add #
Adds a value
method : public : Add(value:JsonElement) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| value | JsonElement | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Insert #
Insert an element
method : public : Insert(name:String, value:Int) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| value | Int | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Example
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
root->Insert("age", 30);
root->Insert("score", 99);
builder->Pop()->ToString()->PrintLine();Insert #
Insert an element
method : public : Insert(name:String, value:Float) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| value | Float | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Example
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
root->Insert("price", 9.99);
root->Insert("tax", 0.08);
builder->Pop()->ToString()->PrintLine();Insert #
Insert an element
method : public : Insert(name:String, value:String) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| value | String | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Example
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
root->Insert("name", "Alice");
root->Insert("role", "admin");
builder->Pop()->ToString()->PrintLine();Insert #
Insert an element
method : public : Insert(name:String, value:Bool) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| value | Bool | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Example
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
root->Insert("active", true);
root->Insert("verified", false);
builder->Pop()->ToString()->PrintLine();Insert #
Insert an element
method : public : Insert(name:String, value:JsonElement) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| name | String | element name |
| value | JsonElement | value |
Return
| Type | Description |
|---|---|
| JsonBuilder | true if successful, false otherwise |
Pop #
Pops a value
method : public : Pop() ~ JsonElementReturn
| Type | Description |
|---|---|
| JsonElement | popped element |
Example
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
root->Insert("x", 1);
json := builder->Pop();
json->ToString()->PrintLine();PopAll #
Pop all values
method : public : PopAll() ~ JsonElementReturn
| Type | Description |
|---|---|
| JsonElement | last popped element |
Example
builder := Data.JSON.JsonBuilder->New();
builder->PushObject()->Insert("a", 1);
json := builder->PopAll();
json->ToString()->PrintLine();PushArray #
Pushes an array element on the stack
method : public : PushArray() ~ JsonBuilderExample
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
arr := root->PushArray("scores");
arr->Add(95);
arr->Add(87);
arr->Add(92);
builder->Pop()->ToString()->PrintLine();PushObject #
Pushes an object element on the stack
method : public : PushObject() ~ JsonBuilderExample
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
root->Insert("status", "ok");
root->Insert("count", 3);
builder->Pop()->ToString()->PrintLine();PushObject #
Pushes an object element on the stack
method : public : PushObject(name:String) ~ JsonBuilderParameters
| Name | Type | Description |
|---|---|---|
| name | String | object name |
Example
builder := Data.JSON.JsonBuilder->New();
root := builder->PushObject();
addr := root->PushObject("address");
addr->Insert("city", "Oakland");
addr->Insert("zip", "94612");
builder->Pop()->ToString()->PrintLine();