std/json

March 14, 2026 ยท View on GitHub

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

  • DOM-style: Hierarchical tree structure of JsonValue nodes.
  • Type-safe Accessors: Check types (is_string, is_number) and unwrap values safely.
  • Automatic Cleanup: Implements the Drop trait for automatic, recursive memory management.
  • Standards Compliant: Supports standard JSON types including objects, arrays, strings, numbers, booleans, and null.

Usage

import "std/json.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 "Serialized: {json_str}";
    
    // Parsing
    let input = "{\"score\": 100}";
    match JsonValue::parse(input) {
        Ok(parsed) => {
            println "Score: {parsed.get(\"score\").unwrap().as_int().unwrap()}";
            // parsed is freed automatically when this block ends
        }
        Err(e) => println "Error: {e}"
    }
} // 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) -> StringReturns a serialized JSON string.

Memory Management

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