pub struct DiffBuilder { /* private fields */ }Expand description
Builder for Diff.
Implementations§
Source§impl DiffBuilder
impl DiffBuilder
Sourcepub fn ignore_paths(&mut self, value: Vec<IgnorePath>) -> &mut Self
pub fn ignore_paths(&mut self, value: Vec<IgnorePath>) -> &mut Self
An array of paths to ignore.
Use DiffBuilder::ignore_path to add them in a more convenient way.
Sourcepub fn equate_empty_arrays(&mut self, value: bool) -> &mut Self
pub fn equate_empty_arrays(&mut self, value: bool) -> &mut Self
If true arrays with a length of zero will be equal, regardless of whether they are nil.
Sourcepub fn approx_float_eq_epsilon(&mut self, value: f64) -> &mut Self
pub fn approx_float_eq_epsilon(&mut self, value: f64) -> &mut Self
If not zero a float comparison will be done using approx::relative_eq.
It’s useful when you want to ignore small differences, e.g. 0.19999999999999 ~ 0.2.
Sourcepub fn approx_date_time_eq_duration(&mut self, value: Duration) -> &mut Self
pub fn approx_date_time_eq_duration(&mut self, value: Duration) -> &mut Self
An acceptable duration difference for the JSON string values that
are valid timestamps. Date approximation will only be executed
when this value is not zero and a string value is a valid rfc3339 date.
Sourcepub fn source(&mut self, value: Value) -> &mut Self
pub fn source(&mut self, value: Value) -> &mut Self
Source JSON value that will be compared with Diff::target.
Examples found in repository?
1fn main() {
2 let obj1 = serde_json::json!({
3 "user": "John",
4 "age": 31
5 });
6
7 let obj2 = serde_json::json!({
8 "user": "John",
9 "age": 33
10 });
11
12 let diff = sjdiff::DiffBuilder::default()
13 .source(obj1)
14 .target(obj2)
15 .build()
16 .unwrap();
17 let diff = diff.compare();
18
19 serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
20}More examples
1fn main() {
2 let obj1 = serde_json::json!({
3 "users": [
4 {
5 "name": "Joe",
6 "age": 43,
7 },
8 {
9 "name": "Ana",
10 "age": 33,
11 "animals": {
12 "type": "dog"
13 }
14 },
15 ]
16 });
17
18 let obj2 = serde_json::json!({
19 "users": [
20 {
21 "name": "Joe",
22 "age": 43,
23 },
24 {
25 "name": "Ana",
26 "age": 33,
27 "animals": {
28 "type": "cat"
29 }
30 },
31 ]
32 });
33
34 let script = r#"
35 let res = target.value_by_path("users.[_].age", curr_path);
36 res == 33
37 "#;
38
39 let diff = sjdiff::DiffBuilder::default()
40 .source(obj1)
41 .target(obj2)
42 .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43 .build();
44 let diff = diff.unwrap().compare();
45 print!("{:?}", diff);
46}Sourcepub fn target(&mut self, value: Value) -> &mut Self
pub fn target(&mut self, value: Value) -> &mut Self
Target JSON value that will be compared with Diff::source.
Examples found in repository?
1fn main() {
2 let obj1 = serde_json::json!({
3 "user": "John",
4 "age": 31
5 });
6
7 let obj2 = serde_json::json!({
8 "user": "John",
9 "age": 33
10 });
11
12 let diff = sjdiff::DiffBuilder::default()
13 .source(obj1)
14 .target(obj2)
15 .build()
16 .unwrap();
17 let diff = diff.compare();
18
19 serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
20}More examples
1fn main() {
2 let obj1 = serde_json::json!({
3 "users": [
4 {
5 "name": "Joe",
6 "age": 43,
7 },
8 {
9 "name": "Ana",
10 "age": 33,
11 "animals": {
12 "type": "dog"
13 }
14 },
15 ]
16 });
17
18 let obj2 = serde_json::json!({
19 "users": [
20 {
21 "name": "Joe",
22 "age": 43,
23 },
24 {
25 "name": "Ana",
26 "age": 33,
27 "animals": {
28 "type": "cat"
29 }
30 },
31 ]
32 });
33
34 let script = r#"
35 let res = target.value_by_path("users.[_].age", curr_path);
36 res == 33
37 "#;
38
39 let diff = sjdiff::DiffBuilder::default()
40 .source(obj1)
41 .target(obj2)
42 .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43 .build();
44 let diff = diff.unwrap().compare();
45 print!("{:?}", diff);
46}Sourcepub fn build(&self) -> Result<Diff, DiffBuilderError>
pub fn build(&self) -> Result<Diff, DiffBuilderError>
Examples found in repository?
1fn main() {
2 let obj1 = serde_json::json!({
3 "user": "John",
4 "age": 31
5 });
6
7 let obj2 = serde_json::json!({
8 "user": "John",
9 "age": 33
10 });
11
12 let diff = sjdiff::DiffBuilder::default()
13 .source(obj1)
14 .target(obj2)
15 .build()
16 .unwrap();
17 let diff = diff.compare();
18
19 serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
20}More examples
1fn main() {
2 let obj1 = serde_json::json!({
3 "users": [
4 {
5 "name": "Joe",
6 "age": 43,
7 },
8 {
9 "name": "Ana",
10 "age": 33,
11 "animals": {
12 "type": "dog"
13 }
14 },
15 ]
16 });
17
18 let obj2 = serde_json::json!({
19 "users": [
20 {
21 "name": "Joe",
22 "age": 43,
23 },
24 {
25 "name": "Ana",
26 "age": 33,
27 "animals": {
28 "type": "cat"
29 }
30 },
31 ]
32 });
33
34 let script = r#"
35 let res = target.value_by_path("users.[_].age", curr_path);
36 res == 33
37 "#;
38
39 let diff = sjdiff::DiffBuilder::default()
40 .source(obj1)
41 .target(obj2)
42 .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43 .build();
44 let diff = diff.unwrap().compare();
45 print!("{:?}", diff);
46}Source§impl DiffBuilder
impl DiffBuilder
Sourcepub fn ignore_path(&mut self, path: &str) -> &mut Self
pub fn ignore_path(&mut self, path: &str) -> &mut Self
Set a JSON path using a string format that you want to ignore during the comparison.
A string path will be parsed to IgnorePath and appended to Diff::ignore_paths.
§Examples
a.[_].c will ignore c key in any element of array a:
{
"a": [{"b": "3", "c": "4"}]
}[_] means any index.
and a.[1].c will ignore c key in the element with index 1.
address.zip will ignore zip key in the address:
{
"address": {
}NOTE: if the element is missing in the source or target and you added
it to ignore paths, the result diff will still show it as a missing one.
Use DiffBuilder::ignore_path_with_missing with ignore_missing set to true instead.
Sourcepub fn ignore_path_with_missing(
&mut self,
path: &str,
ignore_missing: bool,
) -> &mut Self
pub fn ignore_path_with_missing( &mut self, path: &str, ignore_missing: bool, ) -> &mut Self
Adds a path to the ignored ones. ignore_missing indicates whether the element should
be ignored if it’s missing in the source or target.
See documentation for DiffBuilder::ignore_path for usage examples.
Sourcepub fn ignore_path_with_condition(
&mut self,
path: &str,
condition: IgnorePathCondition,
) -> &mut Self
pub fn ignore_path_with_condition( &mut self, path: &str, condition: IgnorePathCondition, ) -> &mut Self
Does the same as DiffBuilder::ignore_path but you can pass a custom script as a condition.
See the example ignore_with_rhai_script.rs to learn how to use it.
Examples found in repository?
1fn main() {
2 let obj1 = serde_json::json!({
3 "users": [
4 {
5 "name": "Joe",
6 "age": 43,
7 },
8 {
9 "name": "Ana",
10 "age": 33,
11 "animals": {
12 "type": "dog"
13 }
14 },
15 ]
16 });
17
18 let obj2 = serde_json::json!({
19 "users": [
20 {
21 "name": "Joe",
22 "age": 43,
23 },
24 {
25 "name": "Ana",
26 "age": 33,
27 "animals": {
28 "type": "cat"
29 }
30 },
31 ]
32 });
33
34 let script = r#"
35 let res = target.value_by_path("users.[_].age", curr_path);
36 res == 33
37 "#;
38
39 let diff = sjdiff::DiffBuilder::default()
40 .source(obj1)
41 .target(obj2)
42 .ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
43 .build();
44 let diff = diff.unwrap().compare();
45 print!("{:?}", diff);
46}Trait Implementations§
Source§impl Clone for DiffBuilder
impl Clone for DiffBuilder
Source§fn clone(&self) -> DiffBuilder
fn clone(&self) -> DiffBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more