pub struct Value { /* private fields */ }
Expand description
An owned JSON value.
Implementations§
source§impl Value
impl Value
sourcepub fn array<'a>(iter: impl IntoIterator<Item = ValueRef<'a>>) -> Self
pub fn array<'a>(iter: impl IntoIterator<Item = ValueRef<'a>>) -> Self
Creates a new JSON array from an iterator of values.
sourcepub fn object<'a>(
iter: impl IntoIterator<Item = (&'a str, ValueRef<'a>)>
) -> Self
pub fn object<'a>( iter: impl IntoIterator<Item = (&'a str, ValueRef<'a>)> ) -> Self
Creates a new JSON object from an iterator of key-value pairs.
sourcepub fn from_text(json: &[u8]) -> Result<Self>
pub fn from_text(json: &[u8]) -> Result<Self>
Deserialize an instance of Value
from bytes of JSON text.
sourcepub fn from_bytes(bytes: &[u8]) -> Self
pub fn from_bytes(bytes: &[u8]) -> Self
Creates a JSON Value
from bytes of jsonbb encoding.
sourcepub fn as_null(&self) -> Option<()>
pub fn as_null(&self) -> Option<()>
If the value is null
, returns ()
. Returns None
otherwise.
§Example
let value = jsonbb::Value::from(());
assert_eq!(value.as_null(), Some(()));
sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
If the value is a boolean, returns the associated bool. Returns None
otherwise.
§Example
let value = jsonbb::Value::from(true);
assert_eq!(value.as_bool(), Some(true));
sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
If the value is an integer, returns the associated i64. Returns None
otherwise.
§Example
let value = jsonbb::Value::from(1i64);
assert_eq!(value.as_i64(), Some(1));
sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
If the value is an integer, returns the associated u64. Returns None
otherwise.
§Example
let value = jsonbb::Value::from(1i64);
assert_eq!(value.as_u64(), Some(1));
sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
If the value is a float, returns the associated f64. Returns None
otherwise.
§Example
let value = jsonbb::Value::from(3.14_f64);
assert_eq!(value.as_f64(), Some(3.14));
sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
If the value is a string, returns the associated str. Returns None
otherwise.
§Example
let value = jsonbb::Value::from("json");
assert_eq!(value.as_str(), Some("json"));
sourcepub fn as_array(&self) -> Option<ArrayRef<'_>>
pub fn as_array(&self) -> Option<ArrayRef<'_>>
If the value is an array, returns the associated array. Returns None
otherwise.
§Example
let value: jsonbb::Value = "[]".parse().unwrap();
assert_eq!(value.as_array().unwrap().len(), 0);
sourcepub fn as_object(&self) -> Option<ObjectRef<'_>>
pub fn as_object(&self) -> Option<ObjectRef<'_>>
If the value is an object, returns the associated map. Returns None
otherwise.
§Example
let value: jsonbb::Value = "{}".parse().unwrap();
assert_eq!(value.as_object().unwrap().len(), 0);
sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true if the value is a null. Returns false otherwise.
§Example
assert!(jsonbb::Value::from(()).is_null());
// The boolean `false` is not null.
assert!(!jsonbb::Value::from(false).is_null());
sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true if the value is a boolean. Returns false otherwise.
§Example
assert!(jsonbb::Value::from(false).is_boolean());
// The string `"false"` is a string, not a boolean.
assert!(!jsonbb::Value::from("false").is_boolean());
sourcepub fn is_number(&self) -> bool
pub fn is_number(&self) -> bool
Returns true if the value is a number. Returns false otherwise.
§Example
assert!(jsonbb::Value::from(1).is_number());
// The string `"1"` is a string, not a number.
assert!(!jsonbb::Value::from("1").is_number());
sourcepub fn is_u64(&self) -> bool
pub fn is_u64(&self) -> bool
Returns true if the value is an integer between zero and u64::MAX
.
§Example
assert!(jsonbb::Value::from(1i64).is_u64());
// Negative integer.
assert!(!jsonbb::Value::from(-1i64).is_u64());
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
.
§Example
assert!(jsonbb::Value::from(1u64).is_i64());
// Greater than i64::MAX.
assert!(!jsonbb::Value::from(u64::MAX).is_i64());
sourcepub fn is_f64(&self) -> bool
pub fn is_f64(&self) -> bool
Returns true if the value is a number that can be represented by f64.
§Example
assert!(jsonbb::Value::from(0f64).is_f64());
// Integer
assert!(!jsonbb::Value::from(1i64).is_f64());
sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Returns true if the value is a string. Returns false otherwise.
§Example
assert!(jsonbb::Value::from("string").is_string());
// The boolean `false` is not a string.
assert!(!jsonbb::Value::from(false).is_string());
sourcepub fn is_object(&self) -> bool
pub fn is_object(&self) -> bool
Returns true if the value is an object. Returns false otherwise.
sourcepub fn get(&self, index: impl Index) -> Option<ValueRef<'_>>
pub fn get(&self, index: impl Index) -> Option<ValueRef<'_>>
Index into a JSON array or object.
A string index can be used to access a value in an object, and a usize index can be used to access an element of an array.
§Example
let object: jsonbb::Value = r#"{"a": 1, "b": 2}"#.parse().unwrap();
assert_eq!(object.get("a").unwrap().to_string(), "1");
assert!(object.get("c").is_none());
assert!(object.get(0).is_none());
let array: jsonbb::Value = r#"["a", "b"]"#.parse().unwrap();
assert_eq!(array.get(0).unwrap().to_string(), "\"a\"");
assert!(array.get(2).is_none());
assert!(array.get("a").is_none());
sourcepub fn pointer<'a>(&'a self, pointer: &str) -> Option<ValueRef<'a>>
pub fn pointer<'a>(&'a self, pointer: &str) -> Option<ValueRef<'a>>
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 ~1
and ~
is replaced by ~0
. The
addressed value is returned and if there is no such value None
is
returned.
For more information read RFC6901.
§Examples
let data = json!({
"x": {
"y": ["z", "zz"]
}
});
assert_eq!(data.pointer("/x/y/1").unwrap(), json!("zz").as_ref());
assert_eq!(data.pointer("/a/b/c"), None);
sourcepub fn array_push(&mut self, value: ValueRef<'_>)
pub fn array_push(&mut self, value: ValueRef<'_>)
Push a value into a JSON array.
This function is O(N)
where N is the number of elements in the array.
§Panics
Panics if the value is not an array.
§Example
let mut array: jsonbb::Value = "[1]".parse().unwrap();
array.array_push(jsonbb::Value::from(()).as_ref());
array.array_push(jsonbb::Value::from(2).as_ref());
array.array_push(jsonbb::Value::from("str").as_ref());
array.array_push(jsonbb::Value::array([]).as_ref());
array.array_push(jsonbb::Value::object([]).as_ref());
assert_eq!(array.to_string(), r#"[1,null,2,"str",[],{}]"#);
Trait Implementations§
source§impl From<&[u8]> for Value
impl From<&[u8]> for Value
source§impl Ord for Value
impl Ord for Value
Compare two JSON values.
The ordering is defined as follows: https://www.postgresql.org/docs/current/datatype-json.html#JSON-INDEXING
§Example
use jsonbb::Value;
// Object > Array > Boolean > Number > String > Null
let v = ["null", r#""str""#, "-1", "0", "3.14", "false", "true", "[]", "{}"];
let v = v.iter().map(|s| s.parse().unwrap()).collect::<Vec<Value>>();
for (i, a) in v.iter().enumerate() {
for b in v.iter().skip(i + 1) {
assert!(a < b);
}
}
// Array with n elements > array with n - 1 elements
let a: Value = r#"[1, 2, 3]"#.parse().unwrap();
let b: Value = r#"[1, 2]"#.parse().unwrap();
assert!(a > b);
// arrays with equal numbers of elements are compared in the order:
// element-1, element-2 ...
let a: Value = r#"[1, 2]"#.parse().unwrap();
let b: Value = r#"[1, 3]"#.parse().unwrap();
assert!(a < b);
// Object with n pairs > object with n - 1 pairs
let a: Value = r#"{"a": 1, "b": 2}"#.parse().unwrap();
let b: Value = r#"{"a": 1}"#.parse().unwrap();
assert!(a > b);
// Objects with equal numbers of pairs are compared in the order:
// key-1, value-1, key-2 ...
let a: Value = r#"{"a": 1, "b": 2}"#.parse().unwrap();
let b: Value = r#"{"a": 2, "b": 1}"#.parse().unwrap();
assert!(a < b);
source§impl PartialEq<Value> for bool
impl PartialEq<Value> for bool
source§impl PartialEq<Value> for f32
impl PartialEq<Value> for f32
source§impl PartialEq<Value> for f64
impl PartialEq<Value> for f64
source§impl PartialEq<Value> for i16
impl PartialEq<Value> for i16
source§impl PartialEq<Value> for i32
impl PartialEq<Value> for i32
source§impl PartialEq<Value> for i64
impl PartialEq<Value> for i64
source§impl PartialEq<Value> for i8
impl PartialEq<Value> for i8
source§impl PartialEq<Value> for isize
impl PartialEq<Value> for isize
source§impl PartialEq<Value> for u16
impl PartialEq<Value> for u16
source§impl PartialEq<Value> for u32
impl PartialEq<Value> for u32
source§impl PartialEq<Value> for u64
impl PartialEq<Value> for u64
source§impl PartialEq<Value> for u8
impl PartialEq<Value> for u8
source§impl PartialEq<Value> for usize
impl PartialEq<Value> for usize
source§impl<'a> PartialEq<bool> for &'a Value
impl<'a> PartialEq<bool> for &'a Value
source§impl<'a> PartialEq<isize> for &'a Value
impl<'a> PartialEq<isize> for &'a Value
source§impl<'a> PartialEq<usize> for &'a Value
impl<'a> PartialEq<usize> for &'a Value
source§impl PartialEq for Value
impl PartialEq for Value
§Example
let a: jsonbb::Value = r#"{"a": 1, "b": 2}"#.parse().unwrap();
let b: jsonbb::Value = r#"{"b": 2, "a": 1.0}"#.parse().unwrap();
assert_eq!(a, b);
source§impl PartialOrd for Value
impl PartialOrd for Value
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more