#serde-json #json #json-object #merge

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

#139 in Encoding

Download history 14085/week @ 2024-08-15 12572/week @ 2024-08-22 13420/week @ 2024-08-29 14536/week @ 2024-09-05 9768/week @ 2024-09-12 14469/week @ 2024-09-19 16727/week @ 2024-09-26 16301/week @ 2024-10-03 15022/week @ 2024-10-10 19911/week @ 2024-10-17 15809/week @ 2024-10-24 15671/week @ 2024-10-31 12973/week @ 2024-11-07 13686/week @ 2024-11-14 16440/week @ 2024-11-21 13646/week @ 2024-11-28

59,307 downloads per month
Used in 21 crates (12 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

~0.5–1MB
~20K SLoC