Skip to main content

Object

Struct Object 

Source
pub struct Object { /* private fields */ }
Expand description

Represents a JSON object.

Implementations§

Source§

impl Object

Source

pub fn add(&self, key: &str, value: &Value) -> Self

Adds a field to an object.

assert_eq!(Some("test".to_string()),
    Object::new().add("test", &String("test".to_string())).get_string("test"));
Source

pub fn add_array(&self, key: &str, value: &Array) -> Self

Adds a field to an object as an array.

assert_eq!(Some(1),
    Object::new()
        .add_array("test", &Array::new().add_integer(1))
        .get_array("test").and_then(|a| a.get_integer(0).ok()?));
Source

pub fn add_bool(&self, key: &str, value: bool) -> Self

Adds a field to an object as a bool.

assert_eq!(Some(true), Object::new().add_bool("test", true).get_bool("test"));
Source

pub fn add_decimal(&self, key: &str, value: f64) -> Self

Adds a field to an object as a decimal.

assert_eq!(Some(3.0), Object::new().add_decimal("test", 3.0).get_decimal("test"));
Source

pub fn add_integer(&self, key: &str, value: i128) -> Self

Adds a field to an object as an integer.

assert_eq!(Some(3), Object::new().add_integer("test", 3).get_integer("test"));
Source

pub fn add_number(&self, key: &str, value: Number) -> Self

Adds a field to an object as a number.

assert_eq!(Some(3),
    Object::new()
        .add_number("test", Integer(3))
        .get_number("test").and_then(|n| n.as_integer()));
Source

pub fn add_object(&self, key: &str, value: &Object) -> Self

Adds a field to an object as an object.

assert_eq!(Some(1),
    Object::new()
        .add_object("test", &Object::new().add_integer("test", 1))
        .get_object("test").and_then(|o| o.get_integer("test")));
Source

pub fn add_string(&self, key: &str, value: &str) -> Self

Adds a field to an object as an integer.

assert_eq!(Some("test".to_string()),
    Object::new().add_string("test", "test").get_string("test"));
Source

pub fn get(&self, key: &str) -> Option<&Value>

Returns a value from the object if it exists at the given key.

let object = Object::new().add_string("test1", "test");

assert_eq!(Some("test".to_string()), object.get_string("test1"));
assert_eq!(None, object.get_string("test2"));
Source

pub fn get_array(&self, key: &str) -> Option<Array>

Returns a value from the object if it exists at the given key and if it is an array. Otherwise, None is returned.

let object = Object::new()
    .add_array("test1", &Array::new().add_integer(3))
    .add_integer("test2", 3);

assert_eq!(Some(3), object.get_array("test1").and_then(|a| a.get_integer(0).ok()?));
assert_eq!(None, object.get_array("test2"));
Source

pub fn get_bool(&self, key: &str) -> Option<bool>

Returns a value from the object if it exists at the given key and if it is a bool. Otherwise, None is returned.

let object = Object::new()
    .add_bool("test1", true)
    .add_integer("test2", 3);

assert_eq!(Some(true), object.get_bool("test1"));
assert_eq!(None, object.get_string("test2"));
Source

pub fn get_decimal(&self, key: &str) -> Option<f64>

Returns a value from the object if it exists at the given key and if it is a decimal. Otherwise, None is returned.

let object = Object::new()
    .add_decimal("test1", 3.0)
    .add_integer("test2", 3);

assert_eq!(Some(3.0), object.get_decimal("test1"));
assert_eq!(None, object.get_string("test2"));
Source

pub fn get_integer(&self, key: &str) -> Option<i128>

Returns a value from the object if it exists at the given key and if it is an integer. Otherwise, None is returned.

let object = Object::new()
    .add_decimal("test1", 3.0)
    .add_integer("test2", 3);

assert_eq!(Some(3.0), object.get_decimal("test1"));
assert_eq!(None, object.get_string("test2"));
Source

pub fn get_number(&self, key: &str) -> Option<Number>

Returns a value from the object if it exists at the given key and if it is a number. Otherwise, None is returned.

let object = Object::new()
    .add_number("test1", Number::Decimal(3.0))
    .add_integer("test2", 3);

assert_eq!(Some(3.0), object.get_number("test1").and_then(|n| n.as_decimal()));
assert_eq!(None, object.get_string("test2"));
Source

pub fn get_object(&self, key: &str) -> Option<Object>

Returns a value from the object if it exists at the given key and if it is an object. Otherwise, None is returned.

let object = Object::new()
    .add_object("test1", &Object::new().add_integer("test", 3))
    .add_integer("test2", 3);

assert_eq!(Some(3), object.get_object("test1").and_then(|a| a.get_integer("test")));
assert_eq!(None, object.get_array("test2"));
Source

pub fn get_string(&self, key: &str) -> Option<String>

Returns a value from the object if it exists at the given key and if it is a string. Otherwise, None is returned.

let object = Object::new()
    .add_string("test1", "test")
    .add_integer("test2", 3);

assert_eq!(Some("test".to_string()), object.get_string("test1"));
assert_eq!(None, object.get_string("test2"));
Source

pub fn has_key(&self, key: &str) -> bool

Returns true if the object has a field with the given key.

let object = Object::new().add_string("test1", "test");

assert_eq!(true, object.has_key("test1"));
assert_eq!(false, object.has_key("test2"));
Source

pub fn iter(&self) -> ObjectIter<'_>

Returns an iterator over the key/value tuples of the object.

let object = Object::new().add_string("test1", "test1").add_integer("test2", 1);

assert_eq!(object, Object::from_iter(object.iter()));
Source

pub fn new() -> Self

Create an empty JSON object.

Source

pub fn remove(&self, key: &str) -> Self

Removes the field with the given key from the object.

let object = Object::new().add_string("test", "test");

assert_eq!(true, object.has_key("test"));
assert_eq!(false, object.remove("test").has_key("test"));

Trait Implementations§

Source§

impl Clone for Object

Source§

fn clone(&self) -> Object

Returns a duplicate 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 Object

Source§

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

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

impl Default for Object

Source§

fn default() -> Self

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

impl Display for Object

Source§

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

Converts a JSON object to a string.

let data = r#"
   {
       "string": "string",
       "int": 43,
       "float": 5.8,
       "boolean": true,
       "object": {"test": "test"},
       "array": [
           "string",
           1,
           3.0,
           false,
           {"test": "test"},
           [1]
       ]
   }"#;

let v: serde_json::Value = serde_json::from_str(data)?;

assert_eq!(Some(v), serde_json::from_str(&Value::from_str(data)?.to_string()).ok());
Source§

impl FromIterator<(String, Value)> for Object

Source§

fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self

Iterates over the key/value pairs of a JSON object.

Source§

impl Hash for Object

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<'a> IntoIterator for &'a Object

Source§

type Item = (String, Value)

The type of the elements being iterated over.
Source§

type IntoIter = ObjectIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> ObjectIter<'a>

Creates an iterator from a value. Read more
Source§

impl PartialEq for Object

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Object

Source§

impl StructuralPartialEq for Object

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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§

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

Source§

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

Source§

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.