Skip to main content

JsonValue

Enum JsonValue 

Source
pub enum JsonValue {
    Null,
    Boolean(bool),
    Number(f64),
    String(String),
    Array(Vec<JsonValue>),
    Object(BTreeMap<String, JsonValue>),
    // some variants omitted
}
Expand description

An in-memory JSON value.

This is the currency of the FieldCodec trait: a codec produces a JsonValue when encoding and is handed one when decoding. The codec itself never deals with JSON syntax — serialising and parsing are handled by this crate.

use capnp_json::JsonValue;

let value = JsonValue::Array(vec![
  JsonValue::String("hello".into()),
  JsonValue::Number(42.0),
  JsonValue::Null,
]);
assert_eq!(value, value.clone());

§Numbers

Number is an f64, matching JSON’s own numeric model. A codec that needs the full 64-bit integer range should encode to String, which is what this crate does for Int64 and UInt64 fields.

§Object ordering

Object is a BTreeMap, so its members are written in sorted key order. JSON objects are unordered by definition, so no order is more correct than another, but a deterministic one matters: the same value must encode to the same bytes every time, or golden-file tests, response caching and anything signing the output stop working. Sorted order is also the canonical form specified by RFC 8785.

Members of schema structs never pass through this map — they are written in schema declaration order — so this affects only the objects a custom FieldCodec builds.

Duplicate keys are rejected when parsing, so an Object decoded by this crate never loses a member.

Variants§

§

Null

JSON null. Also the encoding of a Cap’n Proto Void.

§

Boolean(bool)

JSON true or false.

§

Number(f64)

A JSON number. See the note on numbers.

§

String(String)

A JSON string, already unescaped.

§

Array(Vec<JsonValue>)

A JSON array.

§

Object(BTreeMap<String, JsonValue>)

A JSON object. See the note on ordering.

Trait Implementations§

Source§

impl Clone for JsonValue

Source§

fn clone(&self) -> JsonValue

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JsonValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for JsonValue

Source§

fn eq(&self, other: &JsonValue) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for JsonValue

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.