Format Module
The Language::Format module provides JSON parsing and serialization for data interchange.
xxml
#import Language::Format;JSONObject
Represents a JSON object with key-value pairs.
Creating Objects
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | JSONObject^ | Create empty object |
| parse | json: String^ | JSONObject^ | Parse JSON string (static) |
xxml
// Create empty object and add properties
Instantiate Format::JSONObject^ As <obj> = Format::JSONObject::Constructor();
Run obj.put(String::Constructor("name"), String::Constructor("Alice"));
Run obj.put(String::Constructor("age"), Integer::Constructor(30));
// Parse from JSON string
Instantiate Format::JSONObject^ As <parsed> = Format::JSONObject::parse(
String::Constructor("{\"name\": \"Bob\", \"active\": true}")
);Adding Values
| Method | Parameters | Returns | Description |
|---|---|---|---|
| put | key: String^, value: String^ | None | Add string value |
| put | key: String^, value: Integer^ | None | Add integer value |
| put | key: String^, value: Double^ | None | Add double value |
| put | key: String^, value: Bool^ | None | Add boolean value |
| put | key: String^, value: JSONObject^ | None | Add nested object |
| put | key: String^, value: JSONArray^ | None | Add array value |
Reading Values
| Method | Parameters | Returns | Description |
|---|---|---|---|
| get | key: String^ | String^ | Get as string |
| getString | key: String^ | String^ | Get string value |
| getInt | key: String^ | Integer^ | Get integer value |
| getDouble | key: String^ | Double^ | Get double value |
| getBool | key: String^ | Bool^ | Get boolean value |
| getObject | key: String^ | JSONObject^ | Get nested object |
| getArray | key: String^ | JSONArray^ | Get array value |
xxml
Instantiate Format::JSONObject^ As <user> = Format::JSONObject::parse(
String::Constructor("{\"name\": \"Alice\", \"age\": 30, \"premium\": true}")
);
Instantiate String^ As <name> = user.getString(String::Constructor("name"));
Instantiate Integer^ As <age> = user.getInt(String::Constructor("age"));
Instantiate Bool^ As <isPremium> = user.getBool(String::Constructor("premium"));
Run System::Console::printLine(name); // Alice
Run System::Console::printLine(age.toString()); // 30Object Operations
| Method | Parameters | Returns | Description |
|---|---|---|---|
| has | key: String^ | Bool^ | Check if key exists |
| remove | key: String^ | None | Remove key-value pair |
| keys | — | List<String>^ | Get all keys |
| size | — | Integer^ | Get number of keys |
Serialization
| Method | Parameters | Returns | Description |
|---|---|---|---|
| toString | — | String^ | Compact JSON string |
| toStringPretty | — | String^ | Formatted JSON string |
xxml
Instantiate Format::JSONObject^ As <config> = Format::JSONObject::Constructor();
Run config.put(String::Constructor("debug"), Bool::Constructor(true));
Run config.put(String::Constructor("maxRetries"), Integer::Constructor(3));
Run config.put(String::Constructor("timeout"), Double::Constructor(30.5));
// Compact output
Instantiate String^ As <compact> = config.toString();
// {"debug":true,"maxRetries":3,"timeout":30.5}
// Pretty printed
Instantiate String^ As <pretty> = config.toStringPretty();
// {
// "debug": true,
// "maxRetries": 3,
// "timeout": 30.5
// }JSONArray
Represents a JSON array of values.
Creating Arrays
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | JSONArray^ | Create empty array |
| parse | json: String^ | JSONArray^ | Parse JSON array string (static) |
Adding Values
| Method | Parameters | Returns | Description |
|---|---|---|---|
| add | value: String^ | None | Add string |
| add | value: Integer^ | None | Add integer |
| add | value: Double^ | None | Add double |
| add | value: Bool^ | None | Add boolean |
| add | value: JSONObject^ | None | Add object |
| add | value: JSONArray^ | None | Add nested array |
Reading Values
| Method | Parameters | Returns | Description |
|---|---|---|---|
| get | index: Integer^ | String^ | Get as string |
| getString | index: Integer^ | String^ | Get string at index |
| getInt | index: Integer^ | Integer^ | Get integer at index |
| getDouble | index: Integer^ | Double^ | Get double at index |
| getBool | index: Integer^ | Bool^ | Get boolean at index |
| getObject | index: Integer^ | JSONObject^ | Get object at index |
| getArray | index: Integer^ | JSONArray^ | Get array at index |
Array Operations
| Method | Parameters | Returns | Description |
|---|---|---|---|
| set | index: Integer^, value: ... | None | Replace value at index |
| remove | index: Integer^ | None | Remove value at index |
| size | — | Integer^ | Get array length |
| toString | — | String^ | Compact JSON string |
| toStringPretty | — | String^ | Formatted JSON string |
xxml
// Create array of strings
Instantiate Format::JSONArray^ As <tags> = Format::JSONArray::Constructor();
Run tags.add(String::Constructor("xxml"));
Run tags.add(String::Constructor("programming"));
Run tags.add(String::Constructor("compiler"));
// Parse JSON array
Instantiate Format::JSONArray^ As <numbers> = Format::JSONArray::parse(
String::Constructor("[1, 2, 3, 4, 5]")
);
// Iterate over array
For (Instantiate Integer^ As <i> = Integer::Constructor(0);
i.lessThan(numbers.size()).toBool();
Set i = i.add(Integer::Constructor(1)))
{
Instantiate Integer^ As <num> = numbers.getInt(i);
Run System::Console::printLine(num.toString());
}Note
JSONObject and JSONArray can be nested to create complex data structures. Use
toStringPretty() for human-readable output during debugging.Nested Structures
xxml
// Build a complex JSON structure
Instantiate Format::JSONObject^ As <user> = Format::JSONObject::Constructor();
Run user.put(String::Constructor("id"), Integer::Constructor(123));
Run user.put(String::Constructor("name"), String::Constructor("Alice"));
// Add nested object
Instantiate Format::JSONObject^ As <address> = Format::JSONObject::Constructor();
Run address.put(String::Constructor("city"), String::Constructor("New York"));
Run address.put(String::Constructor("zip"), String::Constructor("10001"));
Run user.put(String::Constructor("address"), address);
// Add array
Instantiate Format::JSONArray^ As <roles> = Format::JSONArray::Constructor();
Run roles.add(String::Constructor("admin"));
Run roles.add(String::Constructor("user"));
Run user.put(String::Constructor("roles"), roles);
Run System::Console::printLine(user.toStringPretty());
// {
// "id": 123,
// "name": "Alice",
// "address": {
// "city": "New York",
// "zip": "10001"
// },
// "roles": ["admin", "user"]
// }Complete Example
json_demo.xxml
1 #import Language::Core; 2 #import Language::Format; 3 #import Language::Collections; 4 #import Language::System; 5 6 [ Entrypoint 7 { 8 // Create a configuration object 9 Instantiate Format::JSONObject^ As <config> = Format::JSONObject::Constructor(); 10 11 // App settings 12 Run config.put(String::Constructor("appName"), String::Constructor("MyApp")); 13 Run config.put(String::Constructor("version"), String::Constructor("1.0.0")); 14 Run config.put(String::Constructor("debug"), Bool::Constructor(true)); 15 16 // Database config 17 Instantiate Format::JSONObject^ As <database> = Format::JSONObject::Constructor(); 18 Run database.put(String::Constructor("host"), String::Constructor("localhost")); 19 Run database.put(String::Constructor("port"), Integer::Constructor(5432)); 20 Run database.put(String::Constructor("name"), String::Constructor("myapp_db")); 21 Run config.put(String::Constructor("database"), database); 22 23 // Feature flags 24 Instantiate Format::JSONArray^ As <features> = Format::JSONArray::Constructor(); 25 Run features.add(String::Constructor("dark_mode")); 26 Run features.add(String::Constructor("notifications")); 27 Run features.add(String::Constructor("analytics")); 28 Run config.put(String::Constructor("features"), features); 29 30 // Output configuration 31 Run System::Console::printLine(String::Constructor("=== Configuration ===")); 32 Run System::Console::printLine(config.toStringPretty()); 33 34 // Parse and read JSON 35 Run System::Console::printLine(String::Constructor("")); 36 Run System::Console::printLine(String::Constructor("=== Reading Values ===")); 37 38 Instantiate String^ As <appName> = config.getString(String::Constructor("appName")); 39 Run System::Console::printLine(String::Constructor("App: ").append(appName)); 40 41 Instantiate Format::JSONObject^ As <db> = config.getObject(String::Constructor("database")); 42 Instantiate Integer^ As <port> = db.getInt(String::Constructor("port")); 43 Run System::Console::printLine(String::Constructor("DB Port: ").append(port.toString())); 44 45 // Check feature flags 46 Instantiate Format::JSONArray^ As <flags> = config.getArray(String::Constructor("features")); 47 Run System::Console::printLine( 48 String::Constructor("Features enabled: ").append(flags.size().toString()) 49 ); 50 51 Exit(0); 52 } 53 ]