json_diff_ng/
lib.rs

1//! # Library for comparing JSON data structures
2//! ## Summary
3//! Main entry points are [`compare_strs`] to compare string slices and [`compare_serde_values`] to compare already parse [`serde_json::Value`]
4//! ## Example:
5//! ```rust
6//! use json_diff_ng::compare_strs;
7//! let data1 = r#"["a",{"c": ["d","f"] },"b"]"#;
8//! let data2 = r#"["b",{"c": ["e","d"] },"a"]"#;
9//! let diffs = compare_strs(data1, data2, true, &[]).unwrap();
10//! assert!(!diffs.is_empty());
11//! let diffs = diffs.unequal_values.get_diffs();
12//!
13//! assert_eq!(diffs.len(), 1);
14//! assert_eq!(
15//!   diffs.first().unwrap().to_string(),
16//!   r#".[0].c.[1].("f" != "e")"#
17//! );
18//! ```
19//! ## How to handle the results
20//! Results are returned in a triple of [`DiffTreeNode`] called [`Mismatch`].
21//! The triple consists of values only on the left side, values only on the right side and values on both sides that differ.
22//! Since tree traversal is not usually what you want to do on client side, [`DiffTreeNode`] offers [`DiffTreeNode::get_diffs`] to retrieve
23//! a flat list of [`DiffEntry`] which is more easily usable. The path in the json is collapsed into a vector of [`PathElement`] which can be used to follow the diff.
24//! Similarly, all diffs after an operation can be collected using [`Mismatch::all_diffs`].
25//!
26//! ### Just print everything
27//!
28//! ```rust
29//! use serde_json::json;
30//! use json_diff_ng::compare_serde_values;
31//! use json_diff_ng::sort::sort_value;
32//! let data1 = json! {["a",{"c": ["d","f"] },"b"]};
33//! let data2 = json! {["b",{"c": ["e","d"] },"a"]};
34//! let diffs = compare_serde_values(&data1, &data2, true, &[]).unwrap();
35//! for (d_type, d_path) in diffs.all_diffs() {
36//!   let _message = format!("{d_type}: {d_path}");
37//! }
38//! ```
39//!
40//! ### Traversing the diff result JSONs
41//! ```rust
42//! use serde_json::json;
43//! use json_diff_ng::compare_serde_values;
44//! use json_diff_ng::sort::sort_value;
45//! let data1 = json! {["a",{"c": ["d","f"] },"b"]};
46//! let data2 = json! {["b",{"c": ["e","d"] },"a"]};
47//! let diffs = compare_serde_values(&data1, &data2, true, &[]).unwrap();
48//! assert!(!diffs.is_empty());
49//! // since we sorted for comparison, if we want to resolve the path, we need a sorted result as well.
50//! let data1_sorted = sort_value(&data1, &[]);
51//! let data2_sorted = sort_value(&data2, &[]);
52//! let all_diffs = diffs.all_diffs();
53//! assert_eq!(all_diffs.len(), 1);
54//! let (_type, diff) = all_diffs.first().unwrap();
55//! let val = diff.resolve(&data1_sorted);
56//! assert_eq!(val.unwrap().as_str().unwrap(), "f");
57//! let val = diff.resolve(&data2_sorted);
58//! assert_eq!(val.unwrap().as_str().unwrap(), "e");
59//! ```
60//!
61
62pub use enums::DiffEntry;
63pub use enums::DiffTreeNode;
64pub use enums::DiffType;
65pub use enums::Error;
66pub use enums::PathElement;
67pub use mismatch::Mismatch;
68pub use process::compare_serde_values;
69pub use process::compare_strs;
70
71pub mod enums;
72pub mod mismatch;
73pub mod process;
74pub mod sort;
75
76pub type Result<T> = std::result::Result<T, Error>;