diff_json 0.1.0

A powerful JSON diff library for Rust
Documentation
  • Coverage
  • 0%
    0 out of 27 items documented0 out of 16 items with examples
  • Size
  • Source code size: 44.08 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.52 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 29s Average build duration of successful builds.
  • all releases: 27s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mark-ycy

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:

[dependencies]
diff_json = "0.1.0"

Usage

Basic Example

use diff_json::{compare_json, DiffFormatter};

let json1 = r#"{"name": "Alice", "age": 30}"#;
let json2 = r#"{"name": "Alice", "age": 31}"#;

let diffs = compare_json(json1, json2).unwrap();

let formatter = DiffFormatter::new();
println!("{}", formatter.format(&diffs));

Working with Values

use diff_json::{compare_values, JsonDiff};
use serde_json::json;

let v1 = json!({"name": "Alice", "age": 30});
let v2 = json!({"name": "Bob", "age": 31});

let diffs = compare_values(&v1, &v2);
for diff in diffs {
    println!("{}", diff);
}

Array Comparison with Order Ignored

use diff_json::JsonDiff;
use serde_json::json;

let v1 = json!([1, 2, 3]);
let v2 = json!([3, 2, 1]);

let differ = JsonDiff::new().ignore_order(true);
let diffs = differ.diff(&v1, &v2);
// diffs will be empty since arrays contain the same elements

Different Output Formats

use diff_json::{compare_values, DiffFormatter};
use serde_json::json;

let v1 = json!({"name": "Alice"});
let v2 = json!({"name": "Bob"});

let diffs = compare_values(&v1, &v2);
let formatter = DiffFormatter::new();

// Plain text format
println!("{}", formatter.format(&diffs));

// Compact format
println!("{}", formatter.format_compact(&diffs));

// JSON format
println!("{}", formatter.format_json(&diffs));

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 new JsonDiff instance
  • ignore_order(bool): Set whether array comparison should ignore order
  • diff(v1: &Value, v2: &Value) -> Vec<Diff>: Compare two values

DiffFormatter

Format diff results in various styles.

  • new(): Create a new formatter
  • indent(&str): Set indentation for formatted output
  • show_values(bool): Control whether to show values in output
  • format(&[Diff]) -> String: Format diffs as plain text
  • format_compact(&[Diff]) -> String: Format diffs in compact style
  • format_json(&[Diff]) -> String: Format diffs as JSON
  • format_colored(&[Diff]) -> String: Format diffs with color indicators

Diff

Represents a single difference between two JSON values.

  • path: String: JSON path to the changed element
  • diff_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:

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:

cargo test

License

MIT OR Apache-2.0