TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/elfrask/cls/llms.txt
Use this file to discover all available pages before exploring further.
json module is part of the CLS core standard library and is available on every node. It provides two functions — parse and stringify — for moving data between JSON text and CLS runtime values. The module is backed by Rust’s serde_json crate, which means it handles the full JSON specification faithfully: objects, arrays, strings, numbers, booleans, and null all round-trip correctly. Custom classes can participate in serialization by defining a __toJson() magic method.
Import
Function Reference
json.parse(text: String) -> Any
Parses a JSON string and returns the corresponding CLS value. The mapping from JSON to CLS is:
| JSON type | CLS value |
|---|---|
null | null |
true / false | bool |
| Integer number | int |
| Decimal number | float |
"string" | String |
[...] array | Array |
{...} object | Record |
RuntimeError if the input is not valid JSON.
json.stringify(val: Any) -> String
Serializes a CLS value to a compact JSON string. The mapping from CLS to JSON is:
| CLS value | JSON output |
|---|---|
null | null |
bool | true / false |
int | integer number |
float | decimal number |
String | "string" |
Array | [...] |
Record | {...} |
class instance with __toJson() | result of __toJson() |
| any other value | null |
Examples
Parsing JSON text
Parsing nested structures
Stringifying a record
Round-trip
__toJson() Magic Method
If you call json.stringify on a class instance that defines a __toJson() method, the interpreter calls that method and serializes its return value instead of the instance itself. This lets you control exactly how your objects appear in JSON output.
Without
__toJson, a class instance will serialize as null because the underlying Value::Object variant has no automatic JSON representation.