Enum Value

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

Represents any valid JSON API value.

Like serde_json::Value, but with spec compliance baked into the type system.

Variants§

§

Null

A null value.

§

Array(Vec<Value>)

An array of values.

§

Bool(bool)

A boolean value.

§

Number(Number)

An integer or floating point value.

§

Object(Map)

A JSON object as a hash table with consistent order. Keys are guarenteed to be a valid member name.

§

String(String)

A string value.

Implementations§

Source§

impl Value

Source

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

Optionally get the underlying vector as a slice. Returns None if the Value is not an array.

§Example
let data = vec![true.into(), false.into()];
let array = Value::Array(data.clone());
let boolean = Value::Bool(true);

assert_eq!(array.as_array(), Some(data.as_slice()));
assert_eq!(boolean.as_array(), None);
Source

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

Optionally get the underlying vector as a mutable slice. Returns None if the Value is not an array.

§Example
let mut data = vec![true.into(), false.into()];
let mut array = Value::Array(data.clone());
let mut boolean = Value::Bool(true);

assert_eq!(array.as_array_mut(), Some(data.as_mut_slice()));
assert_eq!(boolean.as_array_mut(), None);
Source

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

Optionally get the inner boolean value. Returns None if the Value is not a boolean.

§Example
let boolean = Value::Bool(true);
let number = Value::from(3.14);

assert_eq!(boolean.as_bool(), Some(true));
assert_eq!(number.as_bool(), None);
Source

pub fn as_null(&self) -> Option<()>

Returns Some(()) if the Value is null.

§Example
let null = Value::Null;
let text = Value::String("Hello, World!".to_owned());

assert_eq!(null.as_null(), Some(()));
assert_eq!(text.as_null(), None);
Source

pub fn as_object(&self) -> Option<&Map>

Optionally get a reference to the inner map. Returns None if the Value is not an object.

§Example
let data = Map::new();
let object = Value::Object(data.clone());
let number = Value::from(3.14);

assert_eq!(object.as_object(), Some(&data));
assert_eq!(number.as_object(), None);
Source

pub fn as_object_mut(&mut self) -> Option<&mut Map>

Optionally get a mutable reference to the inner map. Returns None if the Value is not an object.

§Example
let mut data = Map::new();
let mut object = Value::Object(data.clone());
let mut number = Value::from(3.14);

assert_eq!(object.as_object_mut(), Some(&mut data));
assert_eq!(number.as_object_mut(), None);
Source

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

Optionally get the underlying string as a string slice. Returns None if the Value is not a string.

§Example
let data = "Hello, World!";
let string = Value::String(data.to_owned());
let number = Value::from(3.14);

assert_eq!(string.as_str(), Some(data));
assert_eq!(number.as_str(), None);
Source

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

Optionally get the underlying number as an f64. Returns None if the Value cannot be represented as an f64.

§Example
let number = Value::from(3.14);
let string = Value::String("Hello, World!".to_owned());

assert_eq!(number.as_f64(), Some(3.14));
assert_eq!(string.as_f64(), None);
Source

pub fn as_i64(&self) -> Option<i64>

Optionally get the underlying number as an i64. Returns None if the Value cannot be represented as an i64.

§Example
let integer = Value::from(10);
let float = Value::from(3.14);

assert_eq!(integer.as_i64(), Some(10));
assert_eq!(float.as_i64(), None);
Source

pub fn as_u64(&self) -> Option<u64>

Optionally get the underlying number as an u64. Returns None if the Value cannot be represented as an u64.

§Example
let positive = Value::from(10);
let negative = Value::from(-10);

assert_eq!(positive.as_u64(), Some(10));
assert_eq!(negative.as_u64(), None);
Source

pub fn is_array(&self) -> bool

Returns true if the Value is an array.

For any Value on which is_array returns true, as_array and as_array_mut are guaranteed to return a reference to the vector representing the array.

§Example
let mut value = Value::from(vec![1, 2, 3]);

assert!(value.is_array());

value.as_array().unwrap();
value.as_array_mut().unwrap();
Source

pub fn is_boolean(&self) -> bool

Returns true if the Value is a boolean.

For any Value on which is_boolean returns true, as_bool is guaranteed to return the boolean value.

§Example
let value = Value::Bool(true);

assert!(value.is_boolean());
value.as_bool().unwrap();
Source

pub fn is_null(&self) -> bool

Returns true if the Value is null.

For any Value on which is_null returns true, as_null is guaranteed to return Some(()).

§Example
let value = Value::Null;

assert!(value.is_null());
value.as_null().unwrap();
Source

pub fn is_number(&self) -> bool

Returns true if the Value is a number.

§Example
assert!(Value::from(3.14).is_number());
Source

pub fn is_object(&self) -> bool

Returns true if the Value is an object.

For any Value on which is_array returns true, as_object and as_object_mut are guaranteed to return a reference to the map representing the object.

§Example
let mut value = Value::Object(Default::default());

assert!(value.is_object());

value.as_object().unwrap();
value.as_object_mut().unwrap();
Source

pub fn is_string(&self) -> bool

Returns true if the Value is a string.

For any Value on which is_string returns true, as_str is guaranteed to return the string slice.

§Example
let value = Value::String("Hello, world!".to_owned());

assert!(value.is_string());
value.as_str().unwrap();
Source

pub fn is_f64(&self) -> bool

Returns true if the Value is a number that can be represented as an f64.

For any Value on which is_f64 returns true, as_f64 is guaranteed to return the floating point value.

Currently this function returns true if and only if both is_i64 and is_u64 return false. This behavior is not a guarantee in the future.

§Example
let value = Value::from(3.14);

assert!(value.is_f64());
value.as_f64().unwrap();
Source

pub fn is_i64(&self) -> bool

Returns true if the Value is an integer between i64::MIN and i64::MAX.

For any Value on which is_i64 returns true, as_i64 is guaranteed to return the integer value.

§Example
let pos = Value::from(3);
let neg = Value::from(-3);

assert!(pos.is_i64());
assert!(neg.is_i64());

pos.as_i64().unwrap();
neg.as_i64().unwrap();
Source

pub fn is_u64(&self) -> bool

Returns true if the Value is an integer between 0 and u64::MAX.

For any Value on which is_u64 returns true, as_u64 is guaranteed to return the integer value.

§Example
let value = Value::from(3);

assert!(value.is_u64());
value.as_u64().unwrap();

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a copy of the value. Read more
1.0.0 · 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

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

impl Default for Value

Returns the Value::Null. This allows for better composition with Option types.

§Example

const MSG: &'static str = "Hello, World!";

let opt = None;
let value = opt.map(Value::String).unwrap_or_default();
assert_eq!(value, Value::Null);

let opt = Some(MSG.to_owned());
let value = opt.map(Value::String).unwrap_or_default();
assert_eq!(value, Value::String(MSG.to_owned()));
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

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

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

impl<'a, T> From<&'a [T]> for Value
where T: Clone + Into<Value>,

Source§

fn from(data: &'a [T]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for Value

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl From<Map> for Value

Source§

fn from(data: Map) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Option<T>> for Value
where T: Into<Value>,

Source§

fn from(data: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for Value
where T: Into<Value>,

Source§

fn from(data: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(inner: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(n: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(n: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(n: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(n: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(n: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(n: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(n: u8) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<(Key, Value)> for Value

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = (Key, Value)>,

Creates a value from an iterator. Read more
Source§

impl<T> FromIterator<T> for Value
where T: Into<Value>,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl FromStr for Value

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(src: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq<bool> for Value

Source§

fn eq(&self, rhs: &bool) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<f32> for Value

Source§

fn eq(&self, rhs: &f32) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<f64> for Value

Source§

fn eq(&self, rhs: &f64) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<i16> for Value

Source§

fn eq(&self, rhs: &i16) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<i32> for Value

Source§

fn eq(&self, rhs: &i32) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<i64> for Value

Source§

fn eq(&self, rhs: &i64) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<i8> for Value

Source§

fn eq(&self, rhs: &i8) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<isize> for Value

Source§

fn eq(&self, rhs: &isize) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for Value

Source§

fn eq(&self, rhs: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<u16> for Value

Source§

fn eq(&self, rhs: &u16) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<u32> for Value

Source§

fn eq(&self, rhs: &u32) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<u64> for Value

Source§

fn eq(&self, rhs: &u64) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<u8> for Value

Source§

fn eq(&self, rhs: &u8) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<usize> for Value

Source§

fn eq(&self, rhs: &usize) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Value

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Value

Source§

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

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

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin 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> 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,