-
Notifications
You must be signed in to change notification settings - Fork 1
π οΈ Json
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.
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.
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.
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.
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.
Json GetJson(String key)Retrieves a nested JSON object.
Parameters:
-
key: Name of the key.
Returns: A Json object containing the nested data.
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.
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.
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).
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.
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.
void Clear()Removes all members from the JSON object.
bool IsValid()Checks if the JSON document is valid.
Returns: True if no parse errors were detected.
String ToString()Serializes the JSON document into a compact string.
Returns: The JSON as a single-line string.
String ToPrettyString()Serializes the JSON document into a formatted string with indentation.
Returns: The JSON as a human-readable string.
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.