Module std::encoding::json

JSON parsing and manipulation.

Parse JSON strings into values, inspect their types, extract fields, and convert back to JSON text.

Examples

import std::encoding::json;

fn main() {
    let val = json.parse("{\"name\": \"Hew\", \"version\": 1}");
    let name = val.get_field("name");
    println(name.get_string());
    name.free();
    val.free();
}

Contents

Functions

Function parse

pub fn parse(s: string) -> Value

Parse a JSON string into a Value.

Preserves the legacy behavior: invalid JSON returns an invalid handle (type_of() == -1). Use try_parse for a structured error result.

Function try_parse

pub fn try_parse(s: string) -> Result<Value, ParseError>

Parse a JSON string into a Value.

Returns Err(ParseError::Invalid(message)) with the native parser message when parsing fails. Match on ParseError::Invalid(message) to recover the original parser message.

Function object

pub fn object() -> Value

Create a new empty JSON object.

Function array

pub fn array() -> Value

Create a new empty JSON array.

Function from_int

pub fn from_int(val: i64) -> Value

Create a JSON Value from an integer.

Function from_float

pub fn from_float(val: f64) -> Value

Create a JSON Value from a float.

Function from_string

pub fn from_string(val: string) -> Value

Create a JSON Value from a string.

Function from_bool

pub fn from_bool(val: bool) -> Value

Create a JSON Value from a boolean.

Function null

pub fn null() -> Value

Create a JSON null Value.

Types

Enum ParseError

Structured error type for JSON parsing failures.

Use Result<Value, ParseError> with try_parse for structured error handling. Import std::encoding::json when matching on the returned error value so you can use the existing unqualified ParseError::Invalid(message) constructor/pattern style.

Variants

Invalid(string)

The input was not valid JSON. Carries the native parser message.

Struct Value

An opaque JSON value.

Represents any JSON type: null, bool, integer, float, string, array, or object. Created by json.parse(s) or by navigating into a parsed value. Call free() before the value goes out of scope; dropping without free() is a resource leak.

Traits

Trait ValueMethods

Methods available on a JSON Value.

Extends the shared opaque-handle contract from std::encoding::wire::value_trait. Canonical methods are redeclared here with JSON-specific docstrings for documentation purposes.

Methods

fn stringify(self: Self) -> string

Serialize the value back to a JSON string.

fn type_of(self: Self) -> i32

Return the type tag (0=null, 1=bool, 2=i64, 3=float, 4=string, 5=array, 6=object).

fn get_bool(self: Self) -> i32

Extract the boolean value (0 or 1).

fn get_int(self: Self) -> i64

Extract the integer value.

fn get_float(self: Self) -> f64

Extract the floating-point value.

fn get_string(self: Self) -> string

Extract the string value.

fn get_field(self: Self, key: string) -> Value

Look up a field by key in a JSON object.

fn array_len(self: Self) -> i64

Return the number of elements in a JSON array.

fn array_get(self: Self, index: i64) -> Value

Return the element at the given index in a JSON array.

fn keys(self: Self) -> Value

Return a JSON array of the object's keys.

fn free(self: Self)

Canonical release path for Value.

Callers MUST invoke free() before the value goes out of scope; dropping without free() is a resource leak.

fn with_string(self: Self, key: string, val: string) -> Value

Set a string field on an object. Returns the object for chaining.

fn with_int(self: Self, key: string, val: i64) -> Value

Set an integer field. Returns the object for chaining.

fn with_float(self: Self, key: string, val: f64) -> Value

Set a float field. Returns the object for chaining.

fn with_bool(self: Self, key: string, val: bool) -> Value

Set a boolean field. Returns the object for chaining.

fn with_null(self: Self, key: string) -> Value

Set a null field. Returns the object for chaining.

fn with(self: Self, key: string, child: Value) -> Value

Set a Value child on an object. The child is consumed. Returns the object.

fn push(self: Self, child: Value) -> Value

Push a Value onto an array. The child is consumed. Returns the array.

fn push_int(self: Self, val: i64) -> Value

Push an integer onto an array. Returns the array.

fn push_string(self: Self, val: string) -> Value

Push a string onto an array. Returns the array.

fn push_float(self: Self, val: f64) -> Value

Push a float onto an array. Returns the array.

fn push_bool(self: Self, val: bool) -> Value

Push a boolean onto an array. Returns the array.

fn push_null(self: Self) -> Value

Push null onto an array. Returns the array.