packages

json

On this page

JSON (RFC 8259). parse reads a document into a Value, to_str / to_pretty write one, and ToJson / FromJson (with #[derive(Json)]) map Nabi types to and from Value, as std's nabion does for NABION. Tokenizer reads large inputs as a stream of Events.

Decode errors carry a JSON Pointer path (/servers/1/port); from_str turns it into line:col by re-reading the document up to that path, so the happy path keeps no positions.

Modules

  • read Pull tokenizer and parser.
  • write JSON text of a Value: compact or indented.

Re-exports

read::{Event, Tokenizer, MAX_DEPTH}Source

pub use read::{Event, Tokenizer, MAX_DEPTH}
  • Event One step of a document, in order.
  • Tokenizer A JSON reader yielding Events.
  • MAX_DEPTH Nesting limit of parse / from_str / Tokenizer::new.

Structs

JsonErrorSource

pub struct JsonError

A read or decode error. line / col (1-based, code points) are 0 when the value did not come from text; path is a JSON Pointer to the failing part (/servers/2/port, empty at the root).

  • pub fn new(msg: str) -> JsonError§

    Source
  • pub fn within(self, seg: str) -> JsonError§

    Source

    self one level down: seg (/name, /3) goes in front of the path.

  • pub fn message(self) -> str§

    Source

    What went wrong, without the position.

  • pub fn line(self) -> u32§

    Source

    1-based line, 0 when the value did not come from text.

  • pub fn col(self) -> u32§

    Source

    1-based column in code points, 0 when the value did not come from text.

  • pub fn path(self) -> str§

    Source

    JSON Pointer to the failing part, empty at the root.

Trait implementations 3
  • impl Display for JsonError
  • impl Debug for JsonError
  • impl Error for JsonError

Enums

ValueSource

pub enum Value

A JSON value. Integers without a fraction or exponent are exact (Int); other numbers are Float. Objects keep insertion order; a repeated key keeps its first position and its last value.

  • Bool(bool)§

    Source
  • Int(int)§

    Source
  • Float(f64)§

    Source
  • Str(str)§

    Source
  • Array(List<Value>)§

    Source
  • Object(Dict<str, Value>)§

    Source
  • pub fn to_str(self) -> str§

    Source

    Compact text. Panics on a NaN / infinite float.

  • pub fn to_pretty(self, indent: usize) -> str§

    Source

    Text with each member on its own line, indent spaces per level (0 is to_str).

  • pub fn kind(self) -> str§

    Source

    What kind of value this is, for messages.

  • pub fn get(self, key: str) -> Value | none§

    Source

    Member key of an object.

  • pub fn at(self, i: usize) -> Value | none§

    Source

    Item i of an array.

  • pub fn pointer(self, p: str) -> Value | none§

    Source

    The value at JSON Pointer p (RFC 6901): "" is the whole value, /a/0 member a then item 0, ~1 / ~0 stand for / / ~. none when there is no such value or p is malformed.

  • pub fn is_null(self) -> bool§

    Source
  • pub fn as_bool(self) -> bool | none§

    Source
  • pub fn as_int(self) -> int | none§

    Source
  • pub fn as_f64(self) -> f64 | none§

    Source

    A number as f64 (an Int rounds to the nearest).

  • pub fn as_str(self) -> str | none§

    Source
  • pub fn as_array(self) -> List<Value> | none§

    Source
  • pub fn as_object(self) -> Dict<str, Value> | none§

    Source
Trait implementations 1
  • impl Display for Value

Traits

ToJsonSource

pub trait ToJson

A Nabi value as JSON.

FromJsonSource

pub trait FromJson

A Nabi value from JSON; the target type decides how v reads.

  • fn from_json(v: Value) throws (JsonError) -> Self§

    Source
  • fn from_missing() throws (JsonError) -> Self§

    Source

    An object member of this type that the document leaves out: an error, except T | none (none).

Functions

escape_tokenSource

pub fn escape_token(k: str) -> str

k as a JSON Pointer reference token (~ -> ~0, / -> ~1).

parseSource

pub fn parse(s: str) throws (JsonError) -> Value

Read one document (nesting up to MAX_DEPTH).

parse_tokensSource

pub fn parse_tokens(t: Tokenizer) throws (JsonError) -> Value

Read one document from a tokenizer (set its max_depth first to change the limit; Tokenizer::from_reader reads a stream).

to_strSource

pub fn to_str<T: ToJson>(x: T) -> str

x as compact JSON. Panics on a NaN / infinite float.

to_prettySource

pub fn to_pretty<T: ToJson>(x: T) -> str

x as JSON indented by 2 spaces.

from_strSource

pub fn from_str<T: FromJson>(s: str) throws (JsonError) -> T

Read a document as a T; errors carry the line:col of the part that did not fit.

to_valueSource

pub fn to_value<T: ToJson>(x: T) -> Value

x as a Value.

from_valueSource

pub fn from_value<T: FromJson>(v: Value) throws (JsonError) -> T

v as a T.