diff_json
A powerful and flexible JSON diff library for Rust that helps you compare two JSON values and identify differences.
Features
- Comprehensive Diff Detection: Detects added, removed, and modified fields in JSON objects
- Array Comparison: Compare arrays with optional order-insensitive matching
- Nested Structure Support: Handles deeply nested JSON objects and arrays
- Multiple Output Formats: Format diffs as plain text, JSON, or compact summaries
- Easy-to-Use API: Simple and intuitive API for quick integration
- Zero Dependencies: Minimal external dependencies (only serde and serde_json)
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Example
use ;
let json1 = r#"{"name": "Alice", "age": 30}"#;
let json2 = r#"{"name": "Alice", "age": 31}"#;
let diffs = compare_json.unwrap;
let formatter = new;
println!;
Working with Values
use ;
use json;
let v1 = json!;
let v2 = json!;
let diffs = compare_values;
for diff in diffs
Array Comparison with Order Ignored
use JsonDiff;
use json;
let v1 = json!;
let v2 = json!;
let differ = new.ignore_order;
let diffs = differ.diff;
// diffs will be empty since arrays contain the same elements
Different Output Formats
use ;
use json;
let v1 = json!;
let v2 = json!;
let diffs = compare_values;
let formatter = new;
// Plain text format
println!;
// Compact format
println!;
// JSON format
println!;
API Reference
compare_json(json1: &str, json2: &str) -> Result<Vec<Diff>, String>
Compare two JSON strings and return a list of differences.
compare_values(v1: &Value, v2: &Value) -> Vec<Diff>
Compare two serde_json::Value instances and return a list of differences.
JsonDiff
Main diff engine with configurable options.
new(): Create a newJsonDiffinstanceignore_order(bool): Set whether array comparison should ignore orderdiff(v1: &Value, v2: &Value) -> Vec<Diff>: Compare two values
DiffFormatter
Format diff results in various styles.
new(): Create a new formatterindent(&str): Set indentation for formatted outputshow_values(bool): Control whether to show values in outputformat(&[Diff]) -> String: Format diffs as plain textformat_compact(&[Diff]) -> String: Format diffs in compact styleformat_json(&[Diff]) -> String: Format diffs as JSONformat_colored(&[Diff]) -> String: Format diffs with color indicators
Diff
Represents a single difference between two JSON values.
path: String: JSON path to the changed elementdiff_type: DiffType: Type of change (Added, Removed, Modified, Moved)old_value: Option<Value>: Original value (if applicable)new_value: Option<Value>: New value (if applicable)
License
This project is licensed under either of:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Examples
See the examples directory for more usage examples.
Testing
Run tests with:
License
MIT OR Apache-2.0