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
impl Value
Sourcepub fn as_array(&self) -> Option<&[Value]>
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);
Sourcepub fn as_array_mut(&mut self) -> Option<&mut [Value]>
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);
Sourcepub fn as_bool(&self) -> Option<bool>
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);
Sourcepub fn as_null(&self) -> Option<()>
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);
Sourcepub fn as_object(&self) -> Option<&Map>
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);
Sourcepub fn as_object_mut(&mut self) -> Option<&mut Map>
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);
Sourcepub fn as_str(&self) -> Option<&str>
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);
Sourcepub fn as_f64(&self) -> Option<f64>
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);
Sourcepub fn as_i64(&self) -> Option<i64>
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);
Sourcepub fn as_u64(&self) -> Option<u64>
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);
Sourcepub fn is_array(&self) -> bool
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();
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Sourcepub fn is_object(&self) -> bool
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();
Sourcepub fn is_f64(&self) -> bool
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();
Sourcepub fn is_i64(&self) -> bool
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();
Trait Implementations§
Source§impl Default for Value
Returns the Value::Null
. This allows for better composition with Option
types.
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()));