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

MethodParametersReturnsDescription
ConstructorJSONObject^Create empty object
parsejson: 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

MethodParametersReturnsDescription
putkey: String^, value: String^NoneAdd string value
putkey: String^, value: Integer^NoneAdd integer value
putkey: String^, value: Double^NoneAdd double value
putkey: String^, value: Bool^NoneAdd boolean value
putkey: String^, value: JSONObject^NoneAdd nested object
putkey: String^, value: JSONArray^NoneAdd array value

Reading Values

MethodParametersReturnsDescription
getkey: String^String^Get as string
getStringkey: String^String^Get string value
getIntkey: String^Integer^Get integer value
getDoublekey: String^Double^Get double value
getBoolkey: String^Bool^Get boolean value
getObjectkey: String^JSONObject^Get nested object
getArraykey: 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());  // 30

Object Operations

MethodParametersReturnsDescription
haskey: String^Bool^Check if key exists
removekey: String^NoneRemove key-value pair
keysList<String>^Get all keys
sizeInteger^Get number of keys

Serialization

MethodParametersReturnsDescription
toStringString^Compact JSON string
toStringPrettyString^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

MethodParametersReturnsDescription
ConstructorJSONArray^Create empty array
parsejson: String^JSONArray^Parse JSON array string (static)

Adding Values

MethodParametersReturnsDescription
addvalue: String^NoneAdd string
addvalue: Integer^NoneAdd integer
addvalue: Double^NoneAdd double
addvalue: Bool^NoneAdd boolean
addvalue: JSONObject^NoneAdd object
addvalue: JSONArray^NoneAdd nested array

Reading Values

MethodParametersReturnsDescription
getindex: Integer^String^Get as string
getStringindex: Integer^String^Get string at index
getIntindex: Integer^Integer^Get integer at index
getDoubleindex: Integer^Double^Get double at index
getBoolindex: Integer^Bool^Get boolean at index
getObjectindex: Integer^JSONObject^Get object at index
getArrayindex: Integer^JSONArray^Get array at index

Array Operations

MethodParametersReturnsDescription
setindex: Integer^, value: ...NoneReplace value at index
removeindex: Integer^NoneRemove value at index
sizeInteger^Get array length
toStringString^Compact JSON string
toStringPrettyString^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]

See Also