json_value_merge

Crates.iojson_value_merge
lib.rsjson_value_merge
version2.0.0
sourcesrc
created_at2020-04-04 15:41:09.415267
updated_at2023-09-14 21:08:17.404556
descriptionInterface to merge serde_json::Value objects.
homepage
repositoryhttps://github.com/jmfiaschi/json_value_merge
max_upload_size
id226301
size36,224
jm.fiaschi (jmfiaschi)

documentation

README

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

Commit count: 37

cargo fmt