Enum Value
pub enum Value
Represents any valid JSON value.
See the serde_json::value module documentation for usage examples.
Variants
-
Null Represents a JSON null value.
# use json; # let v = json!;-
Bool(bool) Represents a JSON boolean.
# use json; # let v = json!;-
Number(Number) Represents a JSON number, whether integer or floating point.
# use json; # let v = json!;-
String(String) Represents a JSON string.
# use json; # let v = json!;-
Array(Vec<Value>) Represents a JSON array.
# use json; # let v = json!;-
Object(Map<String, Value>) Represents a JSON object.
By default the map is backed by a BTreeMap. Enable the
preserve_orderfeature of serde_json to use IndexMap instead, which preserves entries in the order they are inserted into the map. In particular, this allows JSON data to be deserialized into a Value and serialized to a string while retaining the order of map keys in the input.# use json; # let v = json!;
Implementations
impl Value
fn get<I: Index>(&self, index: I) -> Option<&Value>Index into a JSON array or map. A string index can be used to access a value in a map, and a usize index can be used to access an element of an array.
Returns
Noneif the type ofselfdoes not match the type of the index, for example if the index is a string andselfis an array or a number. Also returnsNoneif the given key does not exist in the map or the given index is not within the bounds of the array.# use json; # let object = json!; assert_eq!; let array = json!; assert_eq!; assert_eq!;Square brackets can also be used to index into a value in a more concise way. This returns
Value::Nullin cases wheregetwould have returnedNone.# use json; # let object = json!; assert_eq!; assert_eq!; assert_eq!;fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Value>Mutably index into a JSON array or map. A string index can be used to access a value in a map, and a usize index can be used to access an element of an array.
Returns
Noneif the type ofselfdoes not match the type of the index, for example if the index is a string andselfis an array or a number. Also returnsNoneif the given key does not exist in the map or the given index is not within the bounds of the array.# use json; # let mut object = json!; *object.get_mut.unwrap = json!; let mut array = json!; *array.get_mut.unwrap = json!;fn is_object(&self) -> boolReturns true if the
Valueis an Object. Returns false otherwise.For any Value on which
is_objectreturns true,as_objectandas_object_mutare guaranteed to return the map representation of the object.# use json; # let obj = json!; assert!; assert!; // array, not an object assert!;fn as_object(&self) -> Option<&Map<String, Value>>If the
Valueis an Object, returns the associated Map. Returns None otherwise.# use json; # let v = json!; // The length of `{"nested": true}` is 1 entry. assert_eq!; // The array `["an", "array"]` is not an object. assert_eq!;fn as_object_mut(&mut self) -> Option<&mut Map<String, Value>>If the
Valueis an Object, returns the associated mutable Map. Returns None otherwise.# use json; # let mut v = json!; v.as_object_mut.unwrap.clear; assert_eq!;fn is_array(&self) -> boolReturns true if the
Valueis an Array. Returns false otherwise.For any Value on which
is_arrayreturns true,as_arrayandas_array_mutare guaranteed to return the vector representing the array.# use json; # let obj = json!; assert!; // an object, not an array assert!;fn as_array(&self) -> Option<&Vec<Value>>If the
Valueis an Array, returns the associated vector. Returns None otherwise.# use json; # let v = json!; // The length of `["an", "array"]` is 2 elements. assert_eq!; // The object `{"an": "object"}` is not an array. assert_eq!;fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>If the
Valueis an Array, returns the associated mutable vector. Returns None otherwise.# use json; # let mut v = json!; v.as_array_mut.unwrap.clear; assert_eq!;fn is_string(&self) -> boolReturns true if the
Valueis a String. Returns false otherwise.For any Value on which
is_stringreturns true,as_stris guaranteed to return the string slice.# use json; # let v = json!; assert!; // The boolean `false` is not a string. assert!;fn as_str(&self) -> Option<&str>If the
Valueis a String, returns the associated str. Returns None otherwise.# use json; # let v = json!; assert_eq!; // The boolean `false` is not a string. assert_eq!; // JSON values are printed in JSON representation, so strings are in quotes. // // The value is: "some string" println!; // Rust strings are printed without quotes. // // The value is: some string println!;fn is_number(&self) -> boolReturns true if the
Valueis a Number. Returns false otherwise.# use json; # let v = json!; assert!; // The string `"2"` is a string, not a number. assert!;fn as_number(&self) -> Option<&Number>If the
Valueis a Number, returns the associatedNumber. Returns None otherwise.# use ; # let v = json!; assert_eq!; assert_eq!; assert_eq!; // The string `"4"` is not a number. assert_eq!;fn is_i64(&self) -> boolReturns true if the
Valueis an integer betweeni64::MINandi64::MAX.For any Value on which
is_i64returns true,as_i64is guaranteed to return the integer value.# use json; # let big = i64max_value as u64 + 10; let v = json!; assert!; // Greater than i64::MAX. assert!; // Numbers with a decimal point are not considered integers. assert!;fn is_u64(&self) -> boolReturns true if the
Valueis an integer between zero andu64::MAX.For any Value on which
is_u64returns true,as_u64is guaranteed to return the integer value.# use json; # let v = json!; assert!; // Negative integer. assert!; // Numbers with a decimal point are not considered integers. assert!;fn is_f64(&self) -> boolReturns true if the
Valueis a number that can be represented by f64.For any Value on which
is_f64returns true,as_f64is guaranteed to return the floating point value.Currently this function returns true if and only if both
is_i64andis_u64return false but this is not a guarantee in the future.# use json; # let v = json!; assert!; // Integers. assert!; assert!;fn as_i64(&self) -> Option<i64>If the
Valueis an integer, represent it as i64 if possible. Returns None otherwise.# use json; # let big = i64max_value as u64 + 10; let v = json!; assert_eq!; assert_eq!; assert_eq!;fn as_u64(&self) -> Option<u64>If the
Valueis an integer, represent it as u64 if possible. Returns None otherwise.# use json; # let v = json!; assert_eq!; assert_eq!; assert_eq!;fn as_f64(&self) -> Option<f64>If the
Valueis a number, represent it as f64 if possible. Returns None otherwise.# use json; # let v = json!; assert_eq!; assert_eq!; assert_eq!;fn is_boolean(&self) -> boolReturns true if the
Valueis a Boolean. Returns false otherwise.For any Value on which
is_booleanreturns true,as_boolis guaranteed to return the boolean value.# use json; # let v = json!; assert!; // The string `"false"` is a string, not a boolean. assert!;fn as_bool(&self) -> Option<bool>If the
Valueis a Boolean, returns the associated bool. Returns None otherwise.# use json; # let v = json!; assert_eq!; // The string `"false"` is a string, not a boolean. assert_eq!;fn is_null(&self) -> boolReturns true if the
Valueis a Null. Returns false otherwise.For any Value on which
is_nullreturns true,as_nullis guaranteed to returnSome(()).# use json; # let v = json!; assert!; // The boolean `false` is not null. assert!;fn as_null(&self) -> Option<()>If the
Valueis a Null, returns (). Returns None otherwise.# use json; # let v = json!; assert_eq!; // The boolean `false` is not null. assert_eq!;fn pointer(&self, pointer: &str) -> Option<&Value>Looks up a value by a JSON Pointer.
JSON Pointer defines a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document.
A Pointer is a Unicode string with the reference tokens separated by
/. Inside tokens/is replaced by~1and~is replaced by~0. The addressed value is returned and if there is no such valueNoneis returned.For more information read RFC6901.
Examples
# use json; # let data = json!; assert_eq!; assert_eq!;fn pointer_mut(&mut self, pointer: &str) -> Option<&mut Value>Looks up a value by a JSON Pointer and returns a mutable reference to that value.
JSON Pointer defines a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document.
A Pointer is a Unicode string with the reference tokens separated by
/. Inside tokens/is replaced by~1and~is replaced by~0. The addressed value is returned and if there is no such valueNoneis returned.For more information read RFC6901.
Example of Use
use Value;fn take(&mut self) -> ValueTakes the value out of the
Value, leaving aNullin its place.# use json; # let mut v = json!; assert_eq!; assert_eq!;fn sort_all_objects(&mut self)Reorders the entries of all
Value::Objectnested within this JSON value according tostr's usual ordering.If serde_json's "preserve_order" feature is not enabled, this method does no work because all JSON maps are always kept in a sorted state.
If serde_json's "preserve_order" feature is enabled, this method destroys the original source order or insertion order of the JSON objects in favor of an alphanumerical order that matches how a BTreeMap with the same contents would be ordered.
Trait Implementations
impl Clone for Value
fn clone(&self) -> Self
impl Debug for Value
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result
impl Default for Value
fn default() -> Value
impl Display for Value
fn fmt(&self, f: &mut Formatter<'_>) -> ResultDisplay a JSON value as a string.
# use json; # let json = json!; // Compact format: // // {"city":"London","street":"10 Downing Street"} let compact = format!; assert_eq!; // Pretty format: // // { // "city": "London", // "street": "10 Downing Street" // } let pretty = format!; assert_eq!;
impl Eq for Value
impl From<&str> for Value
fn from(f: &str) -> SelfConvert string slice to
Value::String.Examples
use Value; let s: &str = "lorem"; let x: Value = s.into;
impl From<()> for Value
fn from((): ()) -> SelfConvert
()toValue::Null.Examples
use Value; let u = ; let x: Value = u.into;
impl From<Map<String, Value>> for Value
fn from(f: Map<String, Value>) -> SelfConvert map (with string keys) to
Value::Object.Examples
use ; let mut m = new; m.insert; let x: Value = m.into;
impl From<Number> for Value
fn from(f: Number) -> SelfConvert
NumbertoValue::Number.Examples
use ; let n = from; let x: Value = n.into;
impl From<String> for Value
fn from(f: String) -> SelfConvert
StringtoValue::String.Examples
use Value; let s: String = "lorem".to_owned; let x: Value = s.into;
impl From<bool> for Value
fn from(f: bool) -> SelfConvert boolean to
Value::Bool.Examples
use Value; let b = false; let x: Value = b.into;
impl From<f32> for Value
fn from(f: f32) -> SelfConvert 32-bit floating point number to
Value::Number, orValue::Nullif infinite or NaN.Examples
use Value; let f: f32 = 13.37; let x: Value = f.into;
impl From<f64> for Value
fn from(f: f64) -> SelfConvert 64-bit floating point number to
Value::Number, orValue::Nullif infinite or NaN.Examples
use Value; let f: f64 = 13.37; let x: Value = f.into;
impl From<i16> for Value
fn from(n: i16) -> Self
impl From<i32> for Value
fn from(n: i32) -> Self
impl From<i64> for Value
fn from(n: i64) -> Self
impl From<i8> for Value
fn from(n: i8) -> Self
impl From<isize> for Value
fn from(n: isize) -> Self
impl From<u16> for Value
fn from(n: u16) -> Self
impl From<u32> for Value
fn from(n: u32) -> Self
impl From<u64> for Value
fn from(n: u64) -> Self
impl From<u8> for Value
fn from(n: u8) -> Self
impl From<usize> for Value
fn from(n: usize) -> Self
impl FromStr for Value
type Err = Error;fn from_str(s: &str) -> Result<Value, Error>
impl Hash for Value
fn hash<__H: Hasher>(&self, state: &mut __H)
impl PartialEq for Value
fn eq(&self, other: &Self) -> bool
impl PartialEq<&str> for Value
fn eq(&self, other: &&str) -> bool
impl PartialEq<String> for Value
fn eq(&self, other: &String) -> bool
impl PartialEq<bool> for Value
fn eq(&self, other: &bool) -> bool
impl PartialEq<f32> for Value
fn eq(&self, other: &f32) -> bool
impl PartialEq<f64> for Value
fn eq(&self, other: &f64) -> bool
impl PartialEq<i16> for Value
fn eq(&self, other: &i16) -> bool
impl PartialEq<i32> for Value
fn eq(&self, other: &i32) -> bool
impl PartialEq<i64> for Value
fn eq(&self, other: &i64) -> bool
impl PartialEq<i8> for Value
fn eq(&self, other: &i8) -> bool
impl PartialEq<isize> for Value
fn eq(&self, other: &isize) -> bool
impl PartialEq<str> for Value
fn eq(&self, other: &str) -> bool
impl PartialEq<u16> for Value
fn eq(&self, other: &u16) -> bool
impl PartialEq<u32> for Value
fn eq(&self, other: &u32) -> bool
impl PartialEq<u64> for Value
fn eq(&self, other: &u64) -> bool
impl PartialEq<u8> for Value
fn eq(&self, other: &u8) -> bool
impl PartialEq<usize> for Value
fn eq(&self, other: &usize) -> bool
impl Serialize for Value
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,
impl StructuralPartialEq for Value
impl<'a> From<Cow<'a, str>> for Value
fn from(f: Cow<'a, str>) -> SelfConvert copy-on-write string to
Value::String.Examples
use Value; use Cow; let s: = Borrowed; let x: Value = s.into;use Value; use Cow; let s: = Owned; let x: Value = s.into;
impl<'de> Deserialize<'de> for Value
fn deserialize<D>(deserializer: D) -> Result<Value, D::Error> where D: Deserializer<'de>,
impl<'de> Deserializer<'de> for Value
type Error = Error;fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_enum<V>(self, name: &'static str, variants: &'static [&'static str], visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_tuple_struct<V>(self, _name: &'static str, _len: usize, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_struct<V>(self, _name: &'static str, _fields: &'static [&'static str], visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Error> where V: Visitor<'de>,
impl<'de> IntoDeserializer<'de, Error> for Value
type Deserializer = Value;fn into_deserializer(self) -> Self::Deserializer
impl<I> Index<I> for Value
where
I: Index,
type Output = Value;fn index(&self, index: I) -> &ValueIndex into a
serde_json::Valueusing the syntaxvalue[0]orvalue["k"].Returns
Value::Nullif the type ofselfdoes not match the type of the index, for example if the index is a string andselfis an array or a number. Also returnsValue::Nullif the given key does not exist in the map or the given index is not within the bounds of the array.For retrieving deeply nested values, you should have a look at the
Value::pointermethod.Examples
# use json; # let data = json!; assert_eq!; assert_eq!; assert_eq!; // returns null for undefined values assert_eq!; // does not panic
impl<I> IndexMut<I> for Value
where
I: Index,
fn index_mut(&mut self, index: I) -> &mut ValueWrite into a
serde_json::Valueusing the syntaxvalue[0] = ...orvalue["k"] = ....If the index is a number, the value must be an array of length bigger than the index. Indexing into a value that is not an array or an array that is too small will panic.
If the index is a string, the value must be an object or null which is treated like an empty object. If the key is not already present in the object, it will be inserted with a value of null. Indexing into a value that is neither an object nor null will panic.
Examples
# use json; # let mut data = json!; // replace an existing key data = json!; // insert a new key data = json!; // replace an array value data = json!; // inserted a deeply nested key data = json!; println!;
impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Value
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> SelfCreate a
Value::Objectby collecting an iterator of key-value pairs.Examples
use Value; let v: = vec!; let x: Value = v.into_iter.collect;
impl<T> From<Option<T>> for Value
where
T: Into<Value>,
fn from(opt: Option<T>) -> Self
impl<T: Clone + Into<Value>> From<&[T]> for Value
fn from(f: &[T]) -> SelfConvert a slice to
Value::Array.Examples
use Value; let v: & = &; let x: Value = v.into;
impl<T: Into<Value>> From<Vec<T>> for Value
fn from(f: Vec<T>) -> SelfConvert a
VectoValue::Array.Examples
use Value; let v = vec!; let x: Value = v.into;
impl<T: Into<Value>> FromIterator<T> for Value
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> SelfCreate a
Value::Arrayby collecting an iterator of array elements.Examples
use Value; let v = repeat.take; let x: Value = v.collect;use Value; let v: = vec!; let x: Value = v.into_iter.collect;use FromIterator; use Value; let x: Value = from_iter;
impl<T: Into<Value>, const N: usize> From<[T; N]> for Value
fn from(array: [T; N]) -> Self
Auto Trait Implementations
impl Freeze 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
impl<T> Any for Value
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Value
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Value
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Value
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> DeserializeOwned for Value
where
T: for<'de> Deserialize<'de>,
impl<T> From<T> for Value
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Value
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> ToString for Value
where
T: Display + ?Sized,
fn to_string(&self) -> String
impl<T, U> Into<U> for Value
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Value
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Value
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>