/json_value_merge

This lib give the possibility to merge two json values together

Primary LanguageRustApache License 2.0Apache-2.0

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());
}

Useful link

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