Enum Value
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(alloc::string::String) Represents a JSON string.
# use json; # let v = json!;-
Array(alloc::vec::Vec<Value>) Represents a JSON array.
# use json; # let v = json!;-
Object(Map<alloc::string::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: &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>(self: &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: &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: &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(self: &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: &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: &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(self: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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: &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(self: &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(self: &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(self: &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.
impl Clone for Value
fn clone(self: &Self) -> Value
impl Debug for Value
fn fmt(self: &Self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result
impl Default for Value
fn default() -> Value
impl Display for Value
fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::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 Freeze for Value
impl From for Value
fn from(n: u32) -> Self
impl From for Value
fn from(n: i8) -> Self
impl From for Value
fn from(n: u16) -> Self
impl From for Value
fn from(n: u8) -> Self
impl From for Value
fn from(n: isize) -> Self
impl From for Value
fn from(n: i64) -> Self
impl From for Value
fn from(n: usize) -> Self
impl From for Value
fn from(n: i32) -> Self
impl From for Value
fn from(n: u64) -> Self
impl From for Value
fn from(n: i16) -> Self
impl From for super::Value
fn from(f: String) -> SelfConvert
StringtoValue::String.Examples
use Value; let s: String = "lorem".to_owned; let x: Value = s.into;
impl From for super::Value
fn from(f: bool) -> SelfConvert boolean to
Value::Bool.Examples
use Value; let b = false; let x: Value = b.into;
impl From for super::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 for super::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 for super::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 for super::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 super::Value
fn from(f: Number) -> SelfConvert
NumbertoValue::Number.Examples
use ; let n = from; let x: Value = n.into;
impl From for super::Value
fn from((): ()) -> SelfConvert
()toValue::Null.Examples
use Value; let u = ; let x: Value = u.into;
impl FromStr for crate::value::Value
fn from_str(s: &str) -> Result<Value, Error>
impl Hash for Value
fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)
impl PartialEq for Value
fn eq(self: &Self, other: &i16) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &isize) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &u32) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &f32) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &i32) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &u8) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &u64) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &f64) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &i8) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &i64) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &u16) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &usize) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &bool) -> bool
impl PartialEq for Value
fn eq(self: &Self, other: &Value) -> bool
impl PartialEq for super::Value
fn eq(self: &Self, other: &String) -> bool
impl PartialEq for super::Value
fn eq(self: &Self, other: &&str) -> bool
impl PartialEq for super::Value
fn eq(self: &Self, other: &str) -> bool
impl RefUnwindSafe for Value
impl Send for Value
impl Serialize for crate::value::Value
fn serialize<S>(self: &Self, serializer: S) -> result::Result<<S as >::Ok, <S as >::Error> where S: ::serde::Serializer
impl StructuralPartialEq for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
impl<'a> From for super::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 for crate::value::Value
fn deserialize<D>(deserializer: D) -> Result<Value, <D as >::Error> where D: serde::Deserializer<'de>
impl<'de> Deserializer for crate::value::Value
fn deserialize_any<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_i8<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_i16<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_i32<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_i64<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_i128<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_u8<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_u16<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_u32<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_u64<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_u128<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_f32<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_f64<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_option<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_enum<V>(self: Self, name: &'static str, variants: &'static [&'static str], visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_newtype_struct<V>(self: Self, name: &'static str, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_bool<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_char<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_str<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_string<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_bytes<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_byte_buf<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_unit<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_unit_struct<V>(self: Self, _name: &'static str, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_seq<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_tuple<V>(self: Self, _len: usize, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_tuple_struct<V>(self: Self, _name: &'static str, _len: usize, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_map<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_struct<V>(self: Self, _name: &'static str, _fields: &'static [&'static str], visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_identifier<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>fn deserialize_ignored_any<V>(self: Self, visitor: V) -> Result<<V as >::Value, Error> where V: Visitor<'de>
impl<'de> IntoDeserializer for crate::value::Value
fn into_deserializer(self: Self) -> <Self as >::Deserializer
impl<I> Index for super::Value
fn index(self: &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 for super::Value
fn index_mut(self: &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<alloc::string::String>, V: Into<super::Value>> FromIterator for super::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> Any for Value
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Value
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Value
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Value
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> DeserializeOwned for Value
impl<T> From for Value
fn from(t: T) -> TReturns the argument unchanged.
impl<T> From for super::Value
fn from(opt: Option<T>) -> Self
impl<T> ToOwned for Value
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for Value
fn to_string(self: &Self) -> String
impl<T, U> Into for Value
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Value
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Value
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: Clone + Into<super::Value>> From for super::Value
fn from(f: &[T]) -> SelfConvert a slice to
Value::Array.Examples
use Value; let v: & = &; let x: Value = v.into;
impl<T: Into<super::Value>> From for super::Value
fn from(f: Vec<T>) -> SelfConvert a
VectoValue::Array.Examples
use Value; let v = vec!; let x: Value = v.into;
impl<T: Into<super::Value>> FromIterator for super::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<super::Value>, N: usize> From for super::Value
fn from(array: [T; N]) -> Self