Skip to content
CryoCryo home
Stdlibencoding

json

RFC 8259 JSON text, read into and written from the shared data model Value. Not auto-imported.

Json::parse(text) reads a Str into a Result<Value, JsonError>; Json::to_text(&value) writes an owned String in compact form and Json::to_pretty_text(&value) indents with two spaces. Json::write_text and Json::write_pretty_text append to a String the caller owns instead, so several values can be written into one buffer.

The parser is strict by RFC 8259: no trailing commas, no unquoted keys, no comments. Json::parse_with(text, &options) reads under JsonParseOptions, whose with_comments() takes // and /* */ comments wherever whitespace may stand, and a refusal's position counts them; the writers never write a comment. UTF-8 input is decoded into UTF-8 output, and \uXXXX escapes are decoded into the scalar values they name, surrogate pairs recombined. Objects and arrays may nest at most 256 deep, counted together; deeper input fails with DepthExceeded.

Integers in i64::MIN..=u64::MAX round-trip exactly as Number::Int / Number::UInt; a value containing a . or an exponent becomes Number::Float. Lossy float conversion happens only when the caller asks for as_f64().

A type is read from and written as JSON text in one step through FromJson and ToJson, which every type with FromValue (or ToValue) has. A type gets both from with [FromJson, ToJson], which derives FromValue and ToValue:

import std::encoding::json::{ FromJson, ToJson };

type struct Server with [FromJson, ToJson] {
    public host: String;
    public port: u16;
}

function main() -> int {
    match (Server::from_json("{\"host\": \"example.com\", \"port\": 8080}")) {
        Result::Ok(server) => {
            const text: String = server.to_json();   // {"host":"example.com","port":8080}
            return 0;
        }
        Result::Err(e) => { return 1; }   // at .port: expected a u16, found a string
    }
}

Types

Json

type struct Json {}

Static functions

static parse(text: Str) -> Result<Value, JsonError>;

Read a complete JSON document, or say where it stops being JSON: the JsonError points at the first byte that failed. Trailing whitespace is allowed; trailing non-whitespace is JsonErrorKind::TrailingData. Arrays and objects nest at most 256 deep, counted together (JsonErrorKind::DepthExceeded).

static parse_with(text: Str, options: &JsonParseOptions) -> Result<Value, JsonError>;

Read a complete JSON document as parse does, under options: with JsonParseOptions::with_comments(), a // comment to the end of its line and a /* */ comment may stand wherever whitespace may. A refusal's line and column count the comments' text, so they point into the text as written.

static to_text(value: &Value) -> String;

value as compact JSON text, no whitespace between tokens: {"name":"cryo","tags":[1,2]}.

static to_pretty_text(value: &Value) -> String;

value as JSON text indented by two spaces, object members and array elements one per line; an empty object or array stays on one line.

static write_text(out: mut &String, value: &Value) -> void;

Append value's compact JSON text to out.

static write_pretty_text(out: mut &String, value: &Value) -> void;

Append value's indented JSON text to out, as to_pretty_text writes it.

JsonParseOptions

type struct JsonParseOptions {
    comments: boolean;
}

strict() is what Json::parse reads under; with_comments() also takes // and /* */ comments.

Properties

PropertyTypeNotes
commentsbooleanWhether comments may stand where whitespace may.

Constructors

static strict() -> JsonParseOptions;

RFC 8259 exactly, as Json::parse reads.

static with_comments() -> JsonParseOptions;

RFC 8259 with // and /* */ comments wherever whitespace may stand. The writers never write a comment.

FromJson

type trait FromJson {
    static from_json(text: Str) -> Result<This, FromTextError<JsonError>>;
}

from_json parses the text and reads its value as the type. A refusal is a FromTextError<JsonError>: Parse when the text is not JSON, Decode when its value is not the type.

Constructors

static from_json(text: Str) -> Result<This, FromTextError<JsonError>>;

text read as a value of this type, or FromTextError::Parse with the JsonError where it is not JSON, or FromTextError::Decode with where its value is not this type.

ToJson

type trait ToJson {
    to_json(&this) -> String;
    to_pretty_json(&this) -> String;
}

Methods

to_json(&this) -> String;

This value as compact JSON text, {"name":"cryo","tags":[1,2]}.

to_pretty_json(&this) -> String;

This value as JSON text indented by two spaces, as Json::to_pretty_text writes it.

module encoding::json::error;
module encoding::json::parser;
module encoding::json::serializer;

export std::encoding::json::error::{ JsonError, JsonErrorKind };
FileWhat it holdsImport
errorJsonError, JsonErrorKindimport std::encoding::json::error;

The reader and writer behind Json are internal to the module.