diff --git a/CHANGELOG.md b/CHANGELOG.md index 6576a63..f7519fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,18 +1,21 @@ -# [1.2.0](https://github.com/jmfiaschi/json_value_merge/compare/v1.1.2...v1.2.0) (2023-09-09) +# [2.0.0-beta.2](https://github.com/jmfiaschi/json_value_merge/compare/v2.0.0-beta.1...v2.0.0-beta.2) (2023-09-14) ### Features * **perf:** force new version ([#17](https://github.com/jmfiaschi/json_value_merge/issues/17)) ([ff38437](https://github.com/jmfiaschi/json_value_merge/commit/ff3843743b44b842e56263a9c297e90577b3f9c1)) -# [1.2.0-beta.1](https://github.com/jmfiaschi/json_value_merge/compare/v1.1.2...v1.2.0-beta.1) (2023-09-09) +# [2.0.0-beta.1](https://github.com/jmfiaschi/json_value_merge/compare/v1.1.2...v2.0.0-beta.1) (2023-09-14) -### Features +### Performance Improvements + +* **merge:** apply pointer in arguments and allow to replace array by object ([36ec2f9](https://github.com/jmfiaschi/json_value_merge/commit/36ec2f99eb55a48dfe8fba20c4c145b82a2e215e)) + + +### BREAKING CHANGES -* **makefile:** add version command ([5f15597](https://github.com/jmfiaschi/json_value_merge/commit/5f15597aebb56149b3f41a0fa8f1be234194c70e)) -* **makefile:** add version command ([8e155df](https://github.com/jmfiaschi/json_value_merge/commit/8e155df16166bed832e0bb10407e4bee97f56d0a)) -* **perf:** force new version ([aa7afde](https://github.com/jmfiaschi/json_value_merge/commit/aa7afdee6d45b871cafe8e46993c4ff5e72843a8)) +* **merge:** apply pointer in arguments and remove the previous declaration ## [1.1.1](https://github.com/jmfiaschi/json_value_merge/compare/v1.1.0...v1.1.1) (2021-12-06) diff --git a/Cargo.toml b/Cargo.toml index 1202031..58b51fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "json_value_merge" -version = "1.2.0" +version = "2.0.0-beta.2" authors = ["Jean-Marc Fiaschi "] edition = "2018" description = "Interface to merge serde_json::Value objects." diff --git a/README.md b/README.md index 6499875..5f61d58 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Give an interface to merge two json_serde::Value together. ```Toml [dependencies] -json_value_merge = "1.1" +json_value_merge = "2.0" ``` ## Usage @@ -26,7 +26,7 @@ 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); + first_json_value.merge(&secound_json_value); assert_eq!(r#"["a","b","c"]"#, first_json_value.to_string()); } ``` @@ -44,7 +44,7 @@ use serde_json::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); + first_json_value.merge(&secound_json_value); assert_eq!( r#"[{"value":"a"},{"value":"b"},{"value":"b"},{"value":"c"}]"#, first_json_value.to_string() @@ -63,7 +63,7 @@ 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.clone()); + value_a.merge_in("/my_array", &value_b); assert_eq!(r#"{"my_array":[{"a":"t"},"b","c"]}"#, value_a.to_string()); } ``` @@ -79,7 +79,7 @@ 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.clone()); + value_a.merge_in("/my_array/0", &value_b); assert_eq!(r#"{"my_array":[{"a":"t","b":"c"}]}"#, value_a.to_string()); } ``` @@ -94,22 +94,18 @@ 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); + 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()); } ``` -Warning: If you want to build an object with key value as an number, it's not possible. The object will be an array. -Actually impossible to have for one json pointer "/field/0" an object like "[{}]" or "{"0":{}}" in the same time. -By default the json pointer "/field/0" will build an object like this "[{}]". - -Merge an object in an array with a wrong position will generate an error. +Replace an array by an object: ```rust extern crate json_value_merge; @@ -119,10 +115,8 @@ 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!(result.is_err(), "The result should be an error because it's not possible to find or add an object in an array with a string field exept '*'"); - assert_eq!(r#"[{"array1":[{"field":"value1"}]}]"#,json_value.to_string()); + let result = json_value.merge_in("/other_field", &Value::String("value".to_string())); + assert_eq!(r#"{"other_field":"value"}"#,json_value.to_string()); } ```