Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Null,
    Bool(bool),
    Number(f64),
    String(String),
    Array(Vec<Value>),
    Object(Vec<(String, Value)>),
}
Expand description

A JSON value.

Numbers are finite f64s, each read as the nearest double to its literal, as JSON.parse reads it: an integer above 2^53 is rounded when it is read (which is why no integer type accepts one), -0 and 0 are one value, and a number is not distinguishable from the same number written another way (1e2 and 100), so a document does not round-trip digit for digit. Display writes a number as JSON.stringify does, and so does Serialize, for every finite double. Serialize is written for serde_json’s serializers: a number that is not a whole number below 2^53 goes through serde_json::value::RawValue, which another serializer shows as that type’s marker and not as a number.

An object is a list of pairs in input order, and a duplicate key keeps its first position and its last value, as JSON.parse does. == (from PartialEq) compares objects in that order and numbers as f64s; use Value::json_equal for JSON Schema’s equality, which ignores key order.

Reading goes through serde, so the parser’s own limits apply: serde_json refuses nesting deeper than 128 levels and an unpaired surrogate escape, which JSON.parse and so a JSON Schema validator in JavaScript accept. A value built another way has no depth limit, and Display, json_equal, Drop and validation recurse. Value::get is linear in the size of the object.

Variants§

§

Null

§

Bool(bool)

§

Number(f64)

§

String(String)

§

Array(Vec<Value>)

§

Object(Vec<(String, Value)>)

Implementations§

Source§

impl Value

Source

pub fn get(&self, key: &str) -> Option<&Value>

The value under key when this is an object that has it.

Source

pub fn as_str(&self) -> Option<&str>

Source

pub fn as_bool(&self) -> Option<bool>

Source

pub fn as_f64(&self) -> Option<f64>

Source

pub fn as_array(&self) -> Option<&[Value]>

Source

pub fn as_object(&self) -> Option<&[(String, Value)]>

Source

pub fn type_name(&self) -> &'static str

The JSON Schema name of this value’s type, with every number called number.

Source

pub fn json_equal(&self, other: &Value) -> bool

Whether two values are equal as JSON Schema defines it: objects regardless of key order, numbers by value. Linear in the size of the values, times a logarithm for sorting object keys.

Source

pub fn canonical(&self) -> String

The text of the value with object keys in sorted order, so two values are equal as JSON Schema defines it exactly when their canonical texts are equal.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Value, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Value

Source§

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

Compact JSON text, with numbers written as JSON.stringify writes them.

Source§

impl PartialEq for Value

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl Serialize for Value

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Shape for Value

Any JSON value, kept in order.

Source§

const CLASS: Class = Class::Any

The class of the shape, which decides at compile time which constraints a field of this type may carry.
Source§

fn reference() -> ShapeDef

How a field of this type is described: a named type gives a reference to its definition, a built-in gives its shape inline.
Source§

fn register(_: &mut Registry)

Adds this type and every named type it refers to to registry.
Source§

fn build(value: &Value, _: &mut Path) -> Result<Value, Issues>

Converts a value that already matches this type’s shape. A mismatch means the shape and the conversion disagree, and is reported as an issue rather than a panic.
Source§

const OPTIONAL: bool = false

Whether a field of this type may be absent from its object.
Source§

const IS_OBJECT: bool = false

Whether the type is a struct, which is what a flattened field and a newtype variant of a tagged enum must be.
Source§

const CATCH_ALL: bool = false

Whether the type collects the members of an object that no field names, so a flattened field of it types them.
Source§

fn catch_all_shape() -> Option<ShapeDef>

The shape of those members, for a type with CATCH_ALL.
Source§

const KEYS: &'static [&'static str]

The keys of the object this type is, own and flattened, which a derive checks for duplicates at compile time.
Source§

fn definition() -> ShapeDef

The complete shape of this type, which for a named type is what its definition holds.
Source§

fn register_members(registry: &mut Registry)

Adds the named types this type’s fields refer to, without this type itself, for a type whose fields are merged into another object’s definition.
Source§

fn absent() -> Option<Self>

The value of a field of this type that is absent from its object, for a type that allows that.
Source§

fn definitions() -> Cow<'static, Definitions>

The definitions this type is described by.
Source§

fn build_rest( value: &Value, path: &mut Path, known: &[&str], ) -> Result<Self, Issues>

Converts the members of an object that are not in known, for a type with CATCH_ALL.
Source§

fn from_json(value: &Value) -> Result<Self, Issues>

Validates value against this type’s shape, collecting every issue with its path, then converts it. This is what deserialising a derived type does, so deserialising is validating.
Source§

fn accepts(value: &Value) -> bool

Whether value matches this type’s shape.
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Forget for Value

§

impl Freeze for Value

§

impl Move for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.