Skip to content

πŸ› οΈ Json

Ekrol34 edited this page Nov 25, 2025 · 1 revision

The Json module provides a structured system to parse, manipulate, and serialize JSON data. It serves as a wrapper around the RapidJSON library, making it easier to load JSON from strings or files, access values, modify structures, and save them back to disk in a clean and readable format.

Behaviour

The module is centered around the Json class. It manages a RapidJSON document internally and exposes simple methods to work with JSON objects, arrays, and primitive values. The class supports both reading and writing operations, including nested JSON handling, lists of strings, and lists of JSON objects.

Additionally, the module offers serialization methods to export JSON content as compact or pretty-printed strings, as well as file saving.

Methods

FromString

bool FromString(String json)

Parses a JSON document from a string.

Parameters:

  • json: The JSON content as a string.

Returns: True if parsing was successful, false otherwise.

FromFile

bool FromFile(String path)

Parses a JSON document from a file.

Parameters:

  • path: Path to the file.

Returns: True if parsing was successful, false otherwise.

Get

String Get(String key)

Retrieves the value of a given key as a string. Automatically converts numeric and boolean values.

Parameters:

  • key: Name of the key.

Returns: The value as a string.

Throws: Exception if the key is null or the type is unsupported.

GetJson

Json GetJson(String key)

Retrieves a nested JSON object.

Parameters:

  • key: Name of the key.

Returns: A Json object containing the nested data.

GetStringList

List<String> GetStringList(String key)

Retrieves a JSON array of strings.

Parameters:

  • key: Name of the key.

Returns: A list of strings.

Throws: Exception if the key is missing or contains non-string elements.

GetJsonList

List<Json> GetJsonList(String key)

Retrieves a JSON array of objects.

Parameters:

  • key: Name of the key.

Returns: A list of Json objects.

Throws: Exception if the key is missing or contains non-object elements.

Set

void Set(String key, String value)
void Set(String key, Json value)
void Set(String key, List<String> value)
void Set(String key, List<Json> value)

Adds or updates a value inside the JSON object.

Parameters:

  • key: Name of the key.

  • value: Value to assign (string, nested Json, list of strings, or list of Json objects).

Has

bool Has(String key)

Checks if the JSON object contains a given key.

Parameters:

  • key: Name of the key.

Returns: True if the key exists, false otherwise.

Delete

void Delete(String key)

Removes a key-value pair from the JSON object.

Parameters:

  • key: Name of the key.

Throws: Exception if the key is not found.

Clear

void Clear()

Removes all members from the JSON object.

IsValid

bool IsValid()

Checks if the JSON document is valid.

Returns: True if no parse errors were detected.

ToString

String ToString()

Serializes the JSON document into a compact string.

Returns: The JSON as a single-line string.

ToPrettyString

String ToPrettyString()

Serializes the JSON document into a formatted string with indentation.

Returns: The JSON as a human-readable string.

SaveAsFile

File SaveAsFile(String name, String path = "./")

Saves the JSON document to a file in pretty-printed format.

Parameters:

  • name: File name (without extension).

  • path: Destination directory (defaults to current).

Returns: A File object representing the saved file.

Clone this wiki locally