std/json

The std/json module provides a DOM-style JSON parser and builder implementation for Zen-C. It features a simple API for creating, manipulating, and serializing JSON data with automatic memory management.

Overview

Usage

import "std/json.zc"
import "std/io.zc"

fn main() {
    // Building JSON
    let obj = JsonValue::object();
    obj.set("name", JsonValue::string("Alice"));
    obj.set("age", JsonValue::number(30.0));
    obj.set("active", JsonValue::bool(true));
    
    // Serialization
    let json_str = obj.to_string();
    println(f"Serialized: {json_str}");

    // Parsing (round trip)
    match JsonValue::parse(json_str) {
        Ok(parsed) => {
            let name = (*parsed).get_string("name").unwrap();
            println(f"Name: {name}");
        },
        Err(e) => { println(f"Parse error: {e}"); }
    }
    free(json_str);
} // obj is freed automatically here

Struct Definition

struct JsonValue {
    kind: JsonType;
    // ... internal fields
}

Methods

Construction

MethodSignatureDescription
nullJsonValue::null() -> JsonValueCreates a null value.
boolJsonValue::bool(b: bool) -> JsonValueCreates a boolean value.
numberJsonValue::number(n: double) -> JsonValueCreates a numeric value.
stringJsonValue::string(s: char*) -> JsonValueCreates a string value.
arrayJsonValue::array() -> JsonValueCreates an empty JSON array.
objectJsonValue::object() -> JsonValueCreates an empty JSON object.

Parsing

MethodSignatureDescription
parseJsonValue::parse(json: char*) -> Result<JsonValue*>Parses a JSON string into a heap-allocated tree.

Accessors

MethodSignatureDescription
is_nullis_null(self) -> boolReturns true if the type is null.
is_boolis_bool(self) -> boolReturns true if the type is boolean.
is_numberis_number(self) -> boolReturns true if the type is a number.
is_stringis_string(self) -> boolReturns true if the type is a string.
is_arrayis_array(self) -> boolReturns true if the type is an array.
is_objectis_object(self) -> boolReturns true if the type is an object.
as_stringas_string(self) -> Option<char*>Returns the string pointer if applicable.
as_intas_int(self) -> Option<int>Returns the integer value if applicable.
as_floatas_float(self) -> Option<double>Returns the numeric value if applicable.
as_boolas_bool(self) -> Option<bool>Returns the boolean value if applicable.

Modification

MethodSignatureDescription
pushpush(self, val: JsonValue)Appends a child value to a JSON array.
setset(self, key: char*, val: JsonValue)Inserts or updates a key-value pair in a JSON object.
getget(self, key: char*) -> Option<JsonValue*>Retrieves a child value from an object by key.
atat(self, index: usize) -> Option<JsonValue*>Retrieves a child value from an array by index.

Serialization

MethodSignatureDescription
to_stringto_string(self) -> char*Returns a serialized JSON string (caller frees).

Memory Management

MethodSignatureDescription
freefree(self)Recursively frees the value and all descendant nodes.
Traitimpl Drop for JsonValueAutomatically triggers recursive free() when out of scope.