Module std::encoding::json

JSON values with automatic cleanup and independent copies.

Inspect values with checked accessors. Mutate a var value with set or push; supplied children remain usable and independent.

Contents

Functions

Function parse

pub fn parse(text: string) -> Result<Value, ParseError>

Parse JSON, retaining the parser's diagnostic on failure.

Function object

pub fn object() -> Value

Create an empty Object.

Function array

pub fn array() -> Value

Create an empty Array.

Function from_int

pub fn from_int(val: i64) -> Value

Construct an exact signed integer.

Function from_u64

pub fn from_u64(val: u64) -> Value

Construct an exact unsigned integer, including values above i64's range.

Function from_float

pub fn from_float(val: f64) -> Result<Value, EncodeError>

Construct a finite float; JSON cannot represent NaN or infinity.

Function from_string

pub fn from_string(val: string) -> Value

Function from_bool

pub fn from_bool(val: bool) -> Value

Function null

pub fn null() -> Value

Function encode

pub fn encode(value: T) -> string

Encode any data value as JSON text. Records encode as objects keyed by field name in declaration order.

Function decode

pub fn decode(text: string) -> Result<T, wire.DecodeError>

Decode JSON text as T. A failure names the path of the value that did not fit, for example Type: .tags[1]: expected string, found integer.

Types

Enum Kind

The represented JSON kind, without exposing runtime tags.

Variants

Null
Bool
Integer
Float
String
Array
Object

Enum ParseError

Malformed JSON input.

Variants

Invalid(string)

Enum EncodeError

A value cannot be represented as JSON text.

Variants

Invalid(string)
NonFiniteFloat

Enum AccessError

A checked access or mutation failed without changing the value.

Variants

WrongKind(Kind, Kind)

Expected kind, followed by the actual kind.

IntegerRange

The integer does not fit the requested signed or unsigned type.

InvalidIndex(i64)

The index is negative or exceeds the supported runtime index range.

Struct Value

An owning JSON value. Assignment and extraction produce independent values. Cleanup is automatic; the representation remains private.

Traits

Trait ValueMethods

Methods

fn type_of(self: Self) -> Kind
fn stringify(self: Self) -> Result<string, EncodeError>
fn get_bool(self: Self) -> Result<bool, AccessError>
fn get_int(self: Self) -> Result<i64, AccessError>
fn get_u64(self: Self) -> Result<u64, AccessError>
fn get_float(self: Self) -> Result<f64, AccessError>

Read a float without coercing an integer to a float.

fn get_string(self: Self) -> Result<string, AccessError>
fn get_field(self: Self, key: string) -> Result<Option<Value>, AccessError>

Missing keys return None; an explicit null returns Some(value).

fn array_len(self: Self) -> Result<i64, AccessError>
fn array_get(self: Self, index: i64) -> Result<Option<Value>, AccessError>

Missing elements return None; unsupported indices return InvalidIndex.

fn keys(self: Self) -> Result<Value, AccessError>

Return an independent array of object keys.

fn set(self: Self, key: string, child: Value) -> Result<(), AccessError>

Set a string-keyed field, preserving the supplied child.

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

Append an independent copy of the child.