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§
Implementations§
Source§impl Value
impl Value
Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
The value under key when this is an object that has it.
pub fn as_str(&self) -> Option<&str>
pub fn as_bool(&self) -> Option<bool>
pub fn as_f64(&self) -> Option<f64>
pub fn as_array(&self) -> Option<&[Value]>
pub fn as_object(&self) -> Option<&[(String, Value)]>
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
The JSON Schema name of this value’s type, with every number called number.
Sourcepub fn json_equal(&self, other: &Value) -> bool
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.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Value, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Value, D::Error>
Source§impl Shape for Value
Any JSON value, kept in order.
impl Shape for Value
Any JSON value, kept in order.
Source§const CLASS: Class = Class::Any
const CLASS: Class = Class::Any
Source§fn reference() -> ShapeDef
fn reference() -> ShapeDef
Source§fn build(value: &Value, _: &mut Path) -> Result<Self, Issues>
fn build(value: &Value, _: &mut Path) -> Result<Self, Issues>
Source§const IS_OBJECT: bool = false
const IS_OBJECT: bool = false
Source§const CATCH_ALL: bool = false
const CATCH_ALL: bool = false
Source§fn catch_all_shape() -> Option<ShapeDef>
fn catch_all_shape() -> Option<ShapeDef>
CATCH_ALL.Source§const KEYS: &'static [&'static str]
const KEYS: &'static [&'static str]
Source§fn definition() -> ShapeDef
fn definition() -> ShapeDef
Source§fn register_members(registry: &mut Registry)
fn register_members(registry: &mut Registry)
Source§fn absent() -> Option<Self>
fn absent() -> Option<Self>
Source§fn definitions() -> Cow<'static, Definitions>
fn definitions() -> Cow<'static, Definitions>
Source§fn build_rest(
value: &Value,
path: &mut Path,
known: &[&str],
) -> Result<Self, Issues>
fn build_rest( value: &Value, path: &mut Path, known: &[&str], ) -> Result<Self, Issues>
known, for a type with CATCH_ALL.