jsondiffpatch-rs
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.
Features
- Object Diffing: Compare JSON objects and generate deltas
- Array Diffing: Detect array changes including moves, inserts, and deletes
- Text Diffing: Character-level text comparison using diff-match-patch
- Pipeline Architecture: Modular filter-based processing pipeline
- Delta Operations: Create, apply, and reverse deltas
- Error Handling: Comprehensive error types for robust operation
Quick Start
use DiffPatcher;
use json;
API Reference
Core Types
DiffPatcher
Main entry point for diff and patch operations.
let diffpatcher = new;
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 changes
Options
Configuration options for the diffing process:
use ;
let options = Options ;
Main Methods
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.
Examples
Object Diffing
use DiffPatcher;
use json;
let diffpatcher = new;
let left = json!;
let right = json!;
if let Some = diffpatcher.diff
Array Diffing
let left = json!;
let right = json!;
if let Some = diffpatcher.diff
Text Diffing
let left = json!;
let right = json!;
if let Some = diffpatcher.diff
Applying Patches
let left = json!;
let delta = diffpatcher.diff.unwrap;
if let Some = diffpatcher.patch
Architecture
The library follows a pipeline-based architecture:
Core Components
- Processor: Manages the processing pipeline and context flow
- Pipeline: Defines the interface for processing operations (diff, patch, reverse)
- Contexts: Carry data through the processing pipeline
DiffContext: Handles diff operationsPatchContext: Handles patch operationsReverseContext: Handles reverse operations
- Filters: Handle specific types of data processing
Pipeline Structure
// Diff pipeline
DiffPipeline
// Patch pipeline
PatchPipeline
Error Handling
The library provides comprehensive error handling through JsonDiffPatchError:
use JsonDiffPatchError;
match diffpatcher.diff
Delta Format
The library uses a compact delta format compatible with the original jsondiffpatch:
Serialization
Deltas can be converted to JSON format for storage/transmission:
if let Some = diffpatcher.diff
Magic Numbers
The library uses magic numbers to identify special operations:
0: Deleted items2: Text diff operations3: Array move operations
Status
Current implementation status:
- ✅ Core architecture and pipeline system
- ✅ Basic types and error handling
- ✅ Object diffing and patching
- ✅ Array diffing with move detection
- ✅ Text diffing using diff-match-patch
- ✅ Delta serialization and deserialization
- ✅ Comprehensive test coverage
- 🔄 Reverse operations (planned)
- 🔄 Performance optimizations (ongoing)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see the LICENSE file for details.
Acknowledgments
This project is based on the original jsondiffpatch library