Trait JsonObjectXlf

Source
pub trait JsonObjectXlf {
    // Required methods
    fn insert_s<T: Serialize>(&mut self, k: &str, v: T) -> Option<Json>;
    fn take_with_prefix(&mut self, prefix: &str) -> Self;
    fn merge_to<T, S, K>(&self, dst: &mut T, skip: &S) -> Result<()>
       where T: Serialize + DeserializeOwned,
             S: ?Sized + Contains<K, str>,
             K: Hash + Ord + Eq + Borrow<str>;
    fn deep_update_with(&mut self, source: Json, allow_null: bool);
}
Expand description

Extension for serde_json::Value.

Required Methods§

Source

fn insert_s<T: Serialize>(&mut self, k: &str, v: T) -> Option<Json>

Insert a key-value pair into a JSON object by specifying the key name and the value to be assigned to it.

§Arguments
  • k v: can be primitive data type, such as string references or integers.
§Examples
use serde_json::json;
use xelf::json::*;

let mut jsn = json!({});

jsn.insert_s("name", "tom");
jsn.insert_s("age", 16);

assert_eq!(jsn.get_or("name", ""), "tom");
assert_eq!(jsn.get_or("age", 16), 16);
Source

fn take_with_prefix(&mut self, prefix: &str) -> Self

Collect all fields that have the specified prefix and insert them into a new object, removing the prefix from the field names before insertion.

Source

fn merge_to<T, S, K>(&self, dst: &mut T, skip: &S) -> Result<()>
where T: Serialize + DeserializeOwned, S: ?Sized + Contains<K, str>, K: Hash + Ord + Eq + Borrow<str>,

Merge this JSON object to a serializable object, skip the fields in skip.

Source

fn deep_update_with(&mut self, source: Json, allow_null: bool)

Recursively update all fields of this JSON object with another JSON object.

§Arguments
  • source: the source JSON object.
  • allow_null: If set to true, the function allows updating target fields with null values from the source object. If set to false, it ignores null values from the source object.

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 JsonObjectXlf for Map<String, Value>

Source§

fn insert_s<T: Serialize>(&mut self, k: &str, v: T) -> Option<Json>

Source§

fn take_with_prefix(&mut self, prefix: &str) -> Self

Source§

fn merge_to<T, S, K>(&self, dst: &mut T, skip: &S) -> Result<()>
where T: Serialize + DeserializeOwned, S: ?Sized + Contains<K, str>, K: Hash + Ord + Eq + Borrow<str>,

Source§

fn deep_update_with(&mut self, source: Json, allow_null: bool)

Implementors§