Struct jsonbb::Value

source ·
pub struct Value { /* private fields */ }
Expand description

An owned JSON value.

Implementations§

source§

impl Value

source

pub fn null() -> Self

Returns a null value.

source

pub fn array<'a>(iter: impl IntoIterator<Item = ValueRef<'a>>) -> Self

Creates a new JSON array from an iterator of values.

source

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.

source

pub fn from_text(json: &[u8]) -> Result<Self>

Deserialize an instance of Value from bytes of JSON text.

source

pub fn from_bytes(bytes: &[u8]) -> Self

Creates a JSON Value from bytes of jsonbb encoding.

source

pub fn as_ref(&self) -> ValueRef<'_>

Returns a reference to the value.

source

pub fn as_bytes(&self) -> &[u8]

Returns the value as bytes.

source

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(()));
source

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));
source

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));
source

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));
source

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));
source

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"));
source

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);
source

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);
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

pub fn is_array(&self) -> bool

Returns true if the value is an array. Returns false otherwise.

source

pub fn is_object(&self) -> bool

Returns true if the value is an object. Returns false otherwise.

source

pub fn capacity(&self) -> usize

Returns the capacity of the internal buffer, in bytes.

source

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());
source

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);
source

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 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

source§

fn default() -> Self

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

impl Display for Value

Display a JSON value as a string.

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl From<&[u8]> for Value

Creates a Value from bytes of jsonbb encoding.

If you want to create a Value from JSON text, use FromStr or from_text instead.

source§

fn from(s: &[u8]) -> Self

Converts to this type from the input type.
source§

impl From<&Number> for Value

source§

fn from(n: &Number) -> Self

Converts to this type from the input type.
source§

impl From<&Value> for Value

source§

fn from(value: &Value) -> Self

Converts to this type from the input type.
source§

impl From<&str> for Value

source§

fn from(s: &str) -> Self

Converts to this type from the input type.
source§

impl From<()> for Value

source§

fn from(_: ()) -> Self

Converts to this type from the input type.
source§

impl From<Number> for Value

source§

fn from(value: Number) -> Self

Converts to this type from the input type.
source§

impl From<Value> for Value

source§

fn from(value: Value) -> Self

Converts to this type from the input type.
source§

impl From<Value> for Value

source§

fn from(value: Value) -> Self

Converts to this type from the input type.
source§

impl From<ValueRef<'_>> for Value

source§

fn from(v: ValueRef<'_>) -> Self

Converts to this type from the input type.
source§

impl From<bool> for Value

source§

fn from(v: bool) -> Self

Converts to this type from the input type.
source§

impl From<f32> for Value

source§

fn from(v: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for Value

source§

fn from(v: f64) -> Self

Converts to this type from the input type.
source§

impl From<i16> for Value

source§

fn from(v: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for Value

source§

fn from(v: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Value

source§

fn from(v: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for Value

source§

fn from(v: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for Value

source§

fn from(v: isize) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Value

source§

fn from(v: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for Value

source§

fn from(v: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Value

source§

fn from(v: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Value

source§

fn from(v: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for Value

source§

fn from(v: usize) -> Self

Converts to this type from the input type.
source§

impl FromStr for Value

§

type Err = Error

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

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

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

impl Hash for Value

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

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§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Value> for bool

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for f32

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for f64

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for i16

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for i32

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for i64

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for i8

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for isize

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for u16

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for u32

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for u64

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for u8

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Value> for usize

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<bool> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<f32> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<f64> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<i16> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<i32> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<i64> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<i8> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<isize> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<u16> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<u32> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<u64> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<u8> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<usize> for &'a Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Value

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
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 Eq 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> 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,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.