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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let obj1 = serde_json::json!({
"user": "John",
"age": 31
});
let obj2 = serde_json::json!({
"user": "John",
"age": 33
});
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.build()
.unwrap();
let diff = diff.compare();
serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
}More examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let obj1 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "dog"
}
},
]
});
let obj2 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "cat"
}
},
]
});
let script = r#"
let res = target.value_by_path("users.[_].age", curr_path);
res == 33
"#;
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
.build();
let diff = diff.unwrap().compare();
print!("{:?}", diff);
}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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let obj1 = serde_json::json!({
"user": "John",
"age": 31
});
let obj2 = serde_json::json!({
"user": "John",
"age": 33
});
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.build()
.unwrap();
let diff = diff.compare();
serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
}More examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let obj1 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "dog"
}
},
]
});
let obj2 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "cat"
}
},
]
});
let script = r#"
let res = target.value_by_path("users.[_].age", curr_path);
res == 33
"#;
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
.build();
let diff = diff.unwrap().compare();
print!("{:?}", diff);
}Sourcepub fn build(&self) -> Result<Diff, DiffBuilderError>
pub fn build(&self) -> Result<Diff, DiffBuilderError>
Examples found in repository?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let obj1 = serde_json::json!({
"user": "John",
"age": 31
});
let obj2 = serde_json::json!({
"user": "John",
"age": 33
});
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.build()
.unwrap();
let diff = diff.compare();
serde_json::to_writer_pretty(std::io::stdout(), &diff).unwrap();
}More examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let obj1 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "dog"
}
},
]
});
let obj2 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "cat"
}
},
]
});
let script = r#"
let res = target.value_by_path("users.[_].age", curr_path);
res == 33
"#;
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
.build();
let diff = diff.unwrap().compare();
print!("{:?}", diff);
}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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
fn main() {
let obj1 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "dog"
}
},
]
});
let obj2 = serde_json::json!({
"users": [
{
"name": "Joe",
"age": 43,
},
{
"name": "Ana",
"age": 33,
"animals": {
"type": "cat"
}
},
]
});
let script = r#"
let res = target.value_by_path("users.[_].age", curr_path);
res == 33
"#;
let diff = sjdiff::DiffBuilder::default()
.source(obj1)
.target(obj2)
.ignore_path_with_condition("users.[_].animals.type", sjdiff::IgnorePathCondition::Rhai(script.to_string()))
.build();
let diff = diff.unwrap().compare();
print!("{:?}", diff);
}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