| Crates.io | jsondiffpatch |
| lib.rs | jsondiffpatch |
| version | 0.1.0 |
| created_at | 2025-06-29 09:40:00.148201+00 |
| updated_at | 2025-06-29 09:40:00.148201+00 |
| description | JSON diff & patch (object and array diff, text diff, multiple output formats) |
| homepage | |
| repository | https://github.com/soraxas/jsondiffpatch |
| max_upload_size | |
| id | 1730593 |
| size | 96,958 |
A Rust implementation of JSON diff & patch functionality, providing object and array diff, text diff, and multiple output formats.
This is a Rust clone of the original jsondiffpatch TypeScript library.
use jsondiffpatch_rs::DiffPatcher;
use serde_json::json;
fn main() {
// Create a diff patcher instance
let diffpatcher = DiffPatcher::new(None);
// Create two JSON objects to compare
let left = json!({
"name": "John",
"age": 30,
"city": "New York"
});
let right = json!({
"name": "John",
"age": 31,
"city": "Boston"
});
// Generate a delta
if let Some(delta) = diffpatcher.diff(&left, &right) {
println!("Delta: {:?}", delta);
// Apply the delta to get the right object
if let Some(patched) = diffpatcher.patch(&left, delta) {
println!("Patched: {}", patched);
}
}
}
DiffPatcherMain entry point for diff and patch operations.
let diffpatcher = DiffPatcher::new(Some(options));
Delta<'a>Represents changes between JSON values:
Delta::Added(&'a Value) - A new value was addedDelta::Modified(&'a Value, &'a Value) - A value was changedDelta::Deleted(&'a Value) - A value was removedDelta::Object(HashMap<String, Delta<'a>>) - Object property changesDelta::Array(Vec<(ArrayDeltaIndex, Delta<'a>)>) - Array element changesDelta::Moved { moved_value: Option<&'a Value>, new_index: usize } - Array element was movedDelta::TextDiff(String) - Text-level changesDelta::None - No changesOptionsConfiguration options for the diffing process:
use jsondiffpatch_rs::types::{Options, ArrayOptions, TextDiffOptions};
let options = Options {
match_by_position: Some(false),
arrays: Some(ArrayOptions {
detect_move: Some(true),
include_value_on_move: Some(false),
}),
text_diff: Some(TextDiffOptions {
min_length: Some(60),
}),
clone_diff_values: Some(false),
omit_removed_values: Some(false),
};
diff(left: &'a Value, right: &'a Value) -> Option<Delta<'a>>Generates a delta representing the differences between two JSON values.
patch(left: &Value, delta: Delta) -> Option<Value>Applies a delta to a JSON value to produce the target value.
reverse(delta: &Delta) -> Option<Delta>Reverses a delta to create an inverse delta.
unpatch(right: &Value, delta: &Delta) -> Option<Value>Reverses a delta to get the original value from the target value.
use jsondiffpatch_rs::DiffPatcher;
use serde_json::json;
let diffpatcher = DiffPatcher::new(None);
let left = json!({"a": 1, "b": 2});
let right = json!({"a": 1, "b": 3, "c": 4});
if let Some(delta) = diffpatcher.diff(&left, &right) {
// delta will contain changes for "b" and "c"
println!("Delta: {:?}", delta);
}
let left = json!([1, 2, 3]);
let right = json!([1, 4, 2, 3]);
if let Some(delta) = diffpatcher.diff(&left, &right) {
// delta will contain array changes including moves
println!("Array delta: {:?}", delta);
}
let left = json!("hello world");
let right = json!("hello rust world");
if let Some(delta) = diffpatcher.diff(&left, &right) {
// delta will contain text-level changes using diff-match-patch
println!("Text delta: {:?}", delta);
}
let left = json!({"name": "John", "age": 30});
let delta = diffpatcher.diff(&left, &right).unwrap();
if let Some(patched) = diffpatcher.patch(&left, delta) {
assert_eq!(patched, right);
}
The library follows a pipeline-based architecture:
DiffContext: Handles diff operationsPatchContext: Handles patch operationsReverseContext: Handles reverse operations// Diff pipeline
DiffPipeline -> [ObjectFilter, ArrayFilter, TextFilter, ...]
// Patch pipeline
PatchPipeline -> [ObjectFilter, ArrayFilter, TextFilter, ...]
The library provides comprehensive error handling through JsonDiffPatchError:
use jsondiffpatch_rs::errors::JsonDiffPatchError;
match diffpatcher.diff(&left, &right) {
Some(delta) => println!("Success: {:?}", delta),
None => println!("No differences found"),
}
The library uses a compact delta format compatible with the original jsondiffpatch:
Deltas can be converted to JSON format for storage/transmission:
if let Some(delta) = diffpatcher.diff(&left, &right) {
let serialized = delta.to_serializable();
println!("Serialized delta: {}", serialized);
}
The library uses magic numbers to identify special operations:
0: Deleted items2: Text diff operations3: Array move operationsCurrent implementation status:
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see the LICENSE file for details.
This project is based on the original jsondiffpatch library