AutolangDocs

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

You can 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 level

JSON 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() // Appending items to a JSON array ports.add(Json.fromInt(8080)) ports.add(Json.fromInt(3000)) // Nesting the array inside the object 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, you should use the type-checking methods (like isString() or isNumber()) 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.

Checking Types & Structure

  • has(key: String): Bool - Checks if a JSON object contains the specified key.
  • isObject(): Bool - Returns true if the node is a JSON object.
  • isArray(): Bool - Returns true if the node is a JSON array.
  • isString(): Bool - Returns true if the node is a String.
  • isNumber(): Bool - Returns true if the node is a Number (Int or Float).
  • isBool(): Bool - Returns true if the node is a Boolean.
  • isNull(): Bool - Returns true if the node is explicitly Null.

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 object key.
  • getAt(index: Int): Json - Retrieves a JSON node from an array by index.
  • add(value: Json) - Appends a JSON node to the end of a JSON array.
  • getSize(): Int - Returns the number of elements in a JSON array.

Extraction & Conversion

  • asString(): String - Extracts the node's value as a native String.
  • asInt(): Int - Extracts the node's value as a native Int.
  • asFloat(): Float - Extracts the node's value as a native Float.
  • asBool(): Bool - Extracts the node's value as a native Bool.
  • toIntArray(): Array<Int> - Converts a JSON array of integers to a native Autolang Array.
  • toFloatArray(): Array<Float> - Converts a JSON array of floats to a native Autolang Array.
  • toStringArray(): Array<String> - Converts a JSON array of strings to a native Autolang Array.
  • stringify(indent: Int = -1): String - Serializes the JSON node back into a string. Provide a positive indent value for pretty-printing.