Skip to content

Commit

Permalink
perf(merge): apply pointer in arguments and allow to replace array by…
Browse files Browse the repository at this point in the history
… object

BREAKING CHANGE: merge with pointer and all replace array by object
  • Loading branch information
jmfiaschi authored Sep 14, 2023
2 parents 5d4fad1 + 27695eb commit 052e37f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
15 changes: 9 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json_value_merge"
version = "1.2.0"
version = "2.0.0-beta.2"
authors = ["Jean-Marc Fiaschi <jm.fiaschi@gmail.com>"]
edition = "2018"
description = "Interface to merge serde_json::Value objects."
Expand Down
36 changes: 15 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
```
Expand All @@ -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()
Expand All @@ -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());
}
```
Expand All @@ -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());
}
```
Expand All @@ -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;
Expand All @@ -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());
}
```

Expand Down

0 comments on commit 052e37f

Please sign in to comment.