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

bin+lib serde_json_diff

创建可机器读取的 JSON 差异

3 个不稳定版本

0.2.0 2023 年 8 月 13 日
0.1.1 2023 年 8 月 12 日
0.1.0 2023 年 8 月 12 日

#2135编码

Download history 293/week @ 2024-04-14 378/week @ 2024-04-21 578/week @ 2024-04-28 302/week @ 2024-05-05 303/week @ 2024-05-12 331/week @ 2024-05-19 113/week @ 2024-05-26 435/week @ 2024-06-02 666/week @ 2024-06-09 504/week @ 2024-06-16 710/week @ 2024-06-23 221/week @ 2024-06-30 1140/week @ 2024-07-07 435/week @ 2024-07-14 470/week @ 2024-07-21 944/week @ 2024-07-28

3,004 每月下载量

MIT 许可证

13KB
245 代码行

创建可机器读取的 JSON 差异

用法

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::objectsserde_json_diff::arrays 也专门暴露出来,用于比较 serde_json::Map<String, serde_json::Value>Vec<serde_json::Value>

命令行界面

serde_json_diff my_json_file.json my_other_json_file.json

提示:由于命令名 serde_json_diff 较长,我已在 shell 配置中将其别名化

alias jdiff="serde_json_diff"

示例

比较此文件

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

与此文件

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

结果为以下差异(序列化为 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"
  }
}

依赖项

~0.7–1.8MB
~37K SLoC