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
59,307 downloads per month
Used in 21 crates
(12 directly)
24KB
281 lines
json_value_merge
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
Dependencies
~0.5–1MB
~20K SLoC