#serde-json #json-diff #diff #json

bin+lib serde_json_diff

Create machine-readable JSON diffs

3 unstable releases

0.2.0 Aug 13, 2023
0.1.1 Aug 12, 2023
0.1.0 Aug 12, 2023

#1145 in Encoding

Download history 209/week @ 2024-12-19 50/week @ 2024-12-26 444/week @ 2025-01-02 357/week @ 2025-01-09 314/week @ 2025-01-16 574/week @ 2025-01-23 546/week @ 2025-01-30 651/week @ 2025-02-06 759/week @ 2025-02-13 617/week @ 2025-02-20 468/week @ 2025-02-27 548/week @ 2025-03-06 1167/week @ 2025-03-13 962/week @ 2025-03-20 862/week @ 2025-03-27 1090/week @ 2025-04-03

4,213 downloads per month
Used in 2 crates (via apirquest_core)

MIT license

13KB
245 lines

Create machine-readable JSON diffs

Usage

Library

let a = serde_json::json!({
  "list": [1, 2, 3],
  "object": {"a": "b"}
});

let b = serde_json::json!({
  "list": [1, 2, 3],
  "object": {"a": "b"}
});

assert!(serde_json_diff::values(a, b).is_none());

serde_json_diff::objects and serde_json_diff::arrays are also exposed specifically for comparing serde_json::Map<String, serde_json::Value> and Vec<serde_json::Value>s respectively.

CLI

serde_json_diff my_json_file.json my_other_json_file.json

Tip: Since the command name serde_json_diff is a bit long, I personally have it aliased in my shell config:

alias jdiff="serde_json_diff"

Example

Comparing this file:

{
  "A": "a",
  "B": "a",
  "D": 1,
  "E": 1,
  "F": [],
  "G": ["a", "a"]
}

To this file:

{
  "A": "a",
  "C": "b",
  "D": 2,
  "E": "1",
  "F": [true],
  "G": ["a", "ab"]
}

Results in this diff (serialised as JSON):

{
  "B": {
    "entry_difference": "extra"
  },
  "D": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "scalar",
      "source": 1,
      "target": 2
    }
  },
  "E": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "type",
      "source_type": "number",
      "target_type": "string",
      "target_value": "1"
    }
  },
  "F": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "array",
      "array_difference": "longer",
      "different_pairs": null,
      "missing_elements": 1
    }
  },
  "G": {
    "entry_difference": "value",
    "value_diff": {
      "difference_of": "array",
      "array_difference": "pairs_only",
      "different_pairs": {
        "1": {
          "difference_of": "scalar",
          "source": "a",
          "target": "ab"
        }
      }
    }
  },
  "C": {
    "entry_difference": "missing",
    "value": "b"
  }
}

Dependencies

~0.6–1.7MB
~35K SLoC