sonic_rs

Trait JsonValueMutTrait

Source
pub trait JsonValueMutTrait {
    type ValueType;
    type ObjectType;
    type ArrayType;

    // Required methods
    fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>;
    fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>;
    fn pointer_mut<P: IntoIterator>(
        &mut self,
        path: P,
    ) -> Option<&mut Self::ValueType>
       where P::Item: Index;
    fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>;
}
Expand description

A trait for all mutable JSON values. Used by mutable Value.

The Option<V: JsonValueMutTrait> and Result<V: JsonValueMutTrait, E> also implement this trait. The Option::None or Result::Err(_) will be viewed as a null value.

Required Associated Types§

Required Methods§

Source

fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>

Returns the mutable object if self is an object.

§Examples
use sonic_rs::{JsonValueMutTrait, json};
use sonic_rs::Value;

let mut value: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
let obj = value.as_object_mut().unwrap();
obj["a"] = json!(2);
assert_eq!(value, json!({"a": 2, "b": true}));
Source

fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>

Returns the mutable array if self is an array.

§Examples
use sonic_rs::{json, JsonValueMutTrait, Value};

let mut value: Value = sonic_rs::from_str(r#"[1, 2, 3]"#).unwrap();
let arr = value.as_array_mut().unwrap();
arr[0] = json!(2);
assert_eq!(value, json!([2, 2, 3]));
Source

fn pointer_mut<P: IntoIterator>( &mut self, path: P, ) -> Option<&mut Self::ValueType>
where P::Item: Index,

Looks up a value by a path.

The path is an iterator of multiple keys or indexes. It can be a &[&str], &[usize] or a JsonPointer.

§Examples
let data = json!({
    "x": {
        "y": ["z", "zz"]
    }
});

assert_eq!(data.pointer(&["x", "y"] ).unwrap(), &json!(["z", "zz"]));
assert_eq!(data.pointer(&pointer!["x", "y", 1] ).unwrap(), &json!("zz"));
assert_eq!(data.pointer(&["a", "b"]), None);
Source

fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>

Mutably index into a JSON array or map. A string-like 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 None if the type of self does not match the type of the index, for example if the index is a string and self is an array or a number. Also returns None if the given key does not exist in the map or the given index is not within the bounds of the array.

use sonic_rs::{json, JsonValueMutTrait};

let mut object = json!({ "A": 65, "B": 66, "C": 67 });
*object.get_mut("A").unwrap() = json!(69);

let mut array = json!([ "A", "B", "C" ]);
*array.get_mut(2).unwrap() = json!("D");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<V: JsonValueMutTrait> JsonValueMutTrait for Option<V>

Source§

type ValueType = <V as JsonValueMutTrait>::ValueType

Source§

type ArrayType = <V as JsonValueMutTrait>::ArrayType

Source§

type ObjectType = <V as JsonValueMutTrait>::ObjectType

Source§

fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>

Source§

fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>

Source§

fn pointer_mut<P: IntoIterator>( &mut self, path: P, ) -> Option<&mut Self::ValueType>
where P::Item: Index,

Source§

fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>

Source§

impl<V: JsonValueMutTrait> JsonValueMutTrait for &mut V

Source§

type ValueType = <V as JsonValueMutTrait>::ValueType

Source§

type ArrayType = <V as JsonValueMutTrait>::ArrayType

Source§

type ObjectType = <V as JsonValueMutTrait>::ObjectType

Source§

fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>

Source§

fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>

Source§

fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>

Source§

fn pointer_mut<P: IntoIterator>( &mut self, path: P, ) -> Option<&mut Self::ValueType>
where P::Item: Index,

Source§

impl<V: JsonValueMutTrait, E> JsonValueMutTrait for Result<V, E>

Source§

type ValueType = <V as JsonValueMutTrait>::ValueType

Source§

type ArrayType = <V as JsonValueMutTrait>::ArrayType

Source§

type ObjectType = <V as JsonValueMutTrait>::ObjectType

Source§

fn as_array_mut(&mut self) -> Option<&mut Self::ArrayType>

Source§

fn as_object_mut(&mut self) -> Option<&mut Self::ObjectType>

Source§

fn pointer_mut<P: IntoIterator>( &mut self, path: P, ) -> Option<&mut Self::ValueType>
where P::Item: Index,

Source§

fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self::ValueType>

Implementors§