#serde-json #json #merge #value #json-object #serde #path

json_value_merge

Interface to merge serde_json::Value objects

14 releases (6 stable)

2.0.0 Sep 14, 2023
1.2.0 Sep 9, 2023
1.1.2 Dec 17, 2021
1.1.0 Sep 17, 2021
0.1.1 Apr 5, 2020

#89 in Encoding

Download history 17214/week @ 2024-02-28 20060/week @ 2024-03-06 21082/week @ 2024-03-13 22228/week @ 2024-03-20 17968/week @ 2024-03-27 15058/week @ 2024-04-03 15006/week @ 2024-04-10 17508/week @ 2024-04-17 24868/week @ 2024-04-24 23976/week @ 2024-05-01 20519/week @ 2024-05-08 27575/week @ 2024-05-15 26662/week @ 2024-05-22 28721/week @ 2024-05-29 23026/week @ 2024-06-05 16755/week @ 2024-06-12

100,818 downloads per month
Used in 12 crates (10 directly)

MIT/Apache

24KB
281 lines

json_value_merge

Actions Status semantic-release crates.io

Give an interface to merge two json_serde::Value together.

Installation

[dependencies]
json_value_merge = "2.0"

Usage

Merge two arrays:

extern crate json_value_merge;

use json_value_merge::Merge;
use serde_json::Value;

{
    let mut first_json_value: Value = serde_json::from_str(r#"["a","b"]"#).unwrap();
    let secound_json_value: Value = serde_json::from_str(r#"["b","c"]"#).unwrap();
    first_json_value.merge(&secound_json_value);
    assert_eq!(r#"["a","b","c"]"#, first_json_value.to_string());
}

Merge two objects:

extern crate json_value_merge;

use json_value_merge::Merge;
use serde_json::Value;

{
    let mut first_json_value: Value =
        serde_json::from_str(r#"[{"value":"a"},{"value":"b"}]"#).unwrap();
    let secound_json_value: Value =
        serde_json::from_str(r#"[{"value":"b"},{"value":"c"}]"#).unwrap();
    first_json_value.merge(&secound_json_value);
    assert_eq!(
        r#"[{"value":"a"},{"value":"b"},{"value":"b"},{"value":"c"}]"#,
        first_json_value.to_string()
    );
}

Merge two arrays in a specifique position:

extern crate json_value_merge;

use json_value_merge::Merge;
use serde_json::Value;

{
    let mut value_a: Value = serde_json::from_str(r#"{"my_array":[{"a":"t"}]}"#).unwrap();
    let value_b: Value = serde_json::from_str(r#"["b","c"]"#).unwrap();
    value_a.merge_in("/my_array", &value_b);
    assert_eq!(r#"{"my_array":[{"a":"t"},"b","c"]}"#, value_a.to_string());
}

Merge two objects in a specifique position:

extern crate json_value_merge;

use json_value_merge::Merge;
use serde_json::Value;

{
    let mut value_a: Value = serde_json::from_str(r#"{"my_array":[{"a":"t"}]}"#).unwrap();
    let value_b: Value = serde_json::from_str(r#"{"b":"c"}"#).unwrap();
    value_a.merge_in("/my_array/0", &value_b);
    assert_eq!(r#"{"my_array":[{"a":"t","b":"c"}]}"#, value_a.to_string());
}

Build new object:

extern crate json_value_merge;

use json_value_merge::Merge;
use serde_json::Value;

{
    let mut object: Value = Value::default();
    object.merge_in("/field", &Value::String("value".to_string()));
    object.merge_in("/object", &Value::Object(Map::default()));
    object.merge_in("/array/1", &Value::Object(Map::default()));
    object.merge_in("/array/2", &Value::Array(Vec::default()));
    object.merge_in("/array/*", &Value::String("wildcard".to_string()));
    object.merge_in("/root/*/item", &Value::String("my_item".to_string()));
    object.merge_in("///empty", &Value::Null);
    assert_eq!(r#"{"":{"":{"empty":null}},"array":[{},[],"wildcard"],"field":"value","object":{},"root":[{"item":"my_item"}]}"#, object.to_string());
}

Replace an array by an object:

extern crate json_value_merge;

use json_value_merge::Merge;
use serde_json::Value;

{
    let mut json_value: Value = serde_json::from_str(r#"[{"array1":[{"field":"value1"}]}]"#).unwrap();
    let result = json_value.merge_in("/other_field", &Value::String("value".to_string()));
    assert_eq!(r#"{"other_field":"value"}"#,json_value.to_string());
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

Apache MIT

Dependencies

~355–760KB
~17K SLoC