JSON
The Json class provides powerful, native capabilities for parsing, manipulating, and generating JSON data in Autolang. Under the hood, it efficiently maps to standard C++ JSON handlers, making it both fast and memory-safe.
Parsing & Creating JSON
Parse JSON directly from a string or build JSON structures from scratch using built-in factory methods.
@import("std/json")
// 1. Parsing from a JSON string
val jsonStr = "{\"name\": \"Autolang\", \"fast\": true}"
val parsedData = Json.parse(jsonStr)
// 2. Creating an object from scratch
val user = Json.emptyObject()
user.set("username", Json.fromString("admin"))
user.set("age", Json.fromInt(25))
// 3. Stringifying the result
println(user.stringify(4)) // The argument specifies the indentation levelJSON Arrays & Nested Data
The JSON module natively supports arrays. You can iterate through them, add elements, or quickly convert them into strongly-typed Autolang arrays.
@import("std/json")
val config = Json.emptyObject()
val ports = Json.emptyArray()
ports.add(Json.fromInt(8080))
ports.add(Json.fromInt(3000))
config.set("ports", ports)
// Accessing array elements
val firstPort = config.get("ports").getAt(0).asInt()
println("First port is: " + firstPort)
// Converting to a native Autolang array
val nativePorts = ports.toIntArray()
println("Native array size: " + nativePorts.size())Type Checking & Extraction
Since JSON data is inherently dynamic, use the type-checking methods (isString(), isNumber(), etc.) before extracting values to avoid runtime exceptions.
@import("std/json")
val response = Json.parse("""{
"status": 200,
"message": "OK"
}""")
if (response.has("status")) {
val statusNode = response.get("status")
if (statusNode.isNumber()) {
println("Status code: " + statusNode.asInt())
}
}Methods Reference
Creation & Parsing
Json.parse(text: String): Json— Parses a JSON formatted string.Json.emptyObject(): Json— Creates a new, empty JSON object.Json.emptyArray(): Json— Creates a new, empty JSON array.Json.fromString(value: String): Json— Wraps a native String into a JSON node.Json.fromInt(value: Int): Json— Wraps a native Int into a JSON node.Json.fromFloat(value: Float): Json— Wraps a native Float into a JSON node.Json.fromBool(value: Bool): Json— Wraps a native Bool into a JSON node.Json.nullValue(): Json— Creates a JSON null value node.
Checking Types & Structure
has(key: String): Bool— Checks if a JSON object contains the specified key.isObject(): Bool/isArray(): Bool/isString(): Bool/isNumber(): Bool/isBool(): Bool/isNull(): Bool/isInt(): Bool/isFloat(): Boolkeys(): Array<String>— Returns an array of keys present in the JSON object.
Accessing & Modifying
get(key: String): Json— Retrieves a JSON node by its object key.set(key: String, value: Json)— Sets a JSON node under the specified key.getAt(index: Int): Json— Retrieves a JSON node from an array by index.add(value: Json)— Appends a JSON node to a JSON array.getSize(): Int— Returns the number of elements in a JSON array.remove(key: String): Bool— Removes a field by its key. Returnstrueif successful.removeAt(index: Int): Bool— Removes an array element by index. Returnstrueif successful.clear()— Clears all elements/fields from a JSON array or object.clone(): Json— Creates a deep clone of the JSON node.
Extraction & Conversion
asString(): String/asInt(): Int/asFloat(): Float/asBool(): BooltoIntArray(): Array<Int>/toFloatArray(): Array<Float>/toStringArray(): Array<String>/toBoolArray(): Array<Bool>/toJsonArray(): Array<Json>toString(): String— Converts the JSON value node directly to a String representation.stringify(indent: Int = -1): String— Serializes the JSON node. Provide a positiveindentfor pretty-printing.
Global Conversion Functions
jsonToClass<T>(json: Json): T— Converts a JSON node directly into an instance of classT.jsonToArrayClass<T>(json: Json): Array<T>— Converts a JSON node into an array of class instances.
