JSON
JSON parsing and generation for data interchange. The Format module provides JSONObject and JSONArray classes for working with JSON data.
xxml
#import Language::Core;
#import Language::Format;JSONObject
Key-value object for JSON data.
Constructor
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | JSONObject^ | Create empty JSON object |
Object Operations
| Method | Parameters | Returns | Description |
|---|---|---|---|
| set | key: String^, value: String^ | None | Set key to value |
| get | key: String^ | String^ | Get value or empty string |
| has | key: String^ | Bool^ | Check if key exists |
| remove | key: String^ | None | Remove key |
| size | — | Integer^ | Number of pairs |
xxml
Instantiate Format::JSONObject^ As <obj> = Format::JSONObject::Constructor();
Run obj.set(String::Constructor("name"), String::Constructor("Alice"));
Run obj.set(String::Constructor("city"), String::Constructor("Seattle"));
If (obj.has(String::Constructor("name"))) -> {
Instantiate String^ As <name> = obj.get(String::Constructor("name"));
Run Console::printLine(name); // "Alice"
}Type-Specific Getters
| Method | Parameters | Returns | Description |
|---|---|---|---|
| getInteger | key: String^ | Integer^ | Parse value as integer |
| getBool | key: String^ | Bool^ | Parse value as boolean |
xxml
Run obj.set(String::Constructor("age"), String::Constructor("25"));
Run obj.set(String::Constructor("active"), String::Constructor("true"));
Instantiate Integer^ As <age> = obj.getInteger(String::Constructor("age"));
Instantiate Bool^ As <active> = obj.getBool(String::Constructor("active"));Serialization
| Method | Parameters | Returns | Description |
|---|---|---|---|
| stringify | — | String^ | Convert to JSON string |
xxml
Instantiate String^ As <json> = obj.stringify();
Run Console::printLine(json); // {"name":"Alice","age":"25"}JSONArray
Ordered array of JSON values.
Constructor
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | JSONArray^ | Create empty JSON array |
Array Operations
| Method | Parameters | Returns | Description |
|---|---|---|---|
| add | value: String^ | None | Add to end |
| get | index: Integer^ | String^ | Get value at index |
| remove | index: Integer^ | None | Remove at index |
| size | — | Integer^ | Number of elements |
xxml
Instantiate Format::JSONArray^ As <arr> = Format::JSONArray::Constructor();
Run arr.add(String::Constructor("first"));
Run arr.add(String::Constructor("second"));
Instantiate String^ As <first> = arr.get(Integer::Constructor(0));
Run Console::printLine(first); // "first"Serialization
| Method | Parameters | Returns | Description |
|---|---|---|---|
| stringify | — | String^ | Convert to JSON string |
xxml
Instantiate String^ As <json> = arr.stringify();
Run Console::printLine(json); // ["first","second"]Note
All values in JSONObject and JSONArray are stored as strings. Use type-specific getters (
getInteger, getBool) for automatic parsing.Complete Example
json_example.xxml
1 #import Language::Core; 2 #import Language::Format; 3 #import Language::System; 4 5 [ Entrypoint 6 { 7 // Build a JSON object 8 Instantiate Format::JSONObject^ As <person> = Format::JSONObject::Constructor(); 9 Run person.set(String::Constructor("name"), String::Constructor("Alice")); 10 Run person.set(String::Constructor("age"), String::Constructor("30")); 11 Run person.set(String::Constructor("active"), String::Constructor("true")); 12 13 // Output as JSON 14 Run Console::printLine(String::Constructor("Person JSON:")); 15 Run Console::printLine(person.stringify()); 16 17 // Read values back 18 Instantiate String^ As <name> = person.get(String::Constructor("name")); 19 Instantiate Integer^ As <age> = person.getInteger(String::Constructor("age")); 20 Instantiate Bool^ As <active> = person.getBool(String::Constructor("active")); 21 22 Run Console::printLine(String::Constructor("Name: ").append(name)); 23 Run Console::printLine(String::Constructor("Age: ").append(age.toString())); 24 25 // Build a JSON array 26 Instantiate Format::JSONArray^ As <items> = Format::JSONArray::Constructor(); 27 Run items.add(String::Constructor("apple")); 28 Run items.add(String::Constructor("banana")); 29 Run items.add(String::Constructor("cherry")); 30 31 Run Console::printLine(String::Constructor("Items JSON:")); 32 Run Console::printLine(items.stringify()); 33 34 Exit(0); 35 } 36 ]