1 个不稳定版本

0.1.0 2021 年 9 月 12 日

#1778开发工具

MIT 许可协议

14KB
243

jsondiff

输出 json 语义差异的工具。
"语义"意味着

  • 在比较之前对对象键进行排序
  • 在比较之前对数组进行排序(可选,但目前无法禁用)

安装

$ cargo install jsondiff

用法

test1.json

{
  "b": "bv",
  "arr": [1,2,3],
  "arr2": [
    {"a":  "av", "obj": {"arr": [1,2,3], "z":  "zv"}},
    {"b":  "bv", "obj": {"arr": [2,3,1], "z":  "zv"}},
    {"c":  "cv", "obj": {"arr": [3,2,1], "z":  "zv"}}
  ]
}

test2.json

{
  "b": "bv",
  "arr": [1,3,2],
  "arr2": [
    {"c":  "cv", "obj": {"arr": [2,3,1], "z":  "zv"}},
    {"b":  "bv", "obj": {"arr": [1,2,3], "z":  "zv"}},
    {"a":  "av", "obj": {"arr": [3,2,1], "z":  "zv"}}
  ]
}

test3.json

{
  "b": "bv",
  "arr": [3,2,1],
  "arr2": [
    {"c":  "cv", "obj": {"arr": [1,2,3], "z":  "zv"}},
    {"b":  "bv", "obj": {"arr": [2,3,1], "z":  "zv"}},
    {"a":  "av", "obj": {"arr": [3,2,1,4], "z":  "zv"}}
  ]
}
$ jsondiff test1.json test2.json
// => no output (no difference)

$ jsondiff test1.json test3.json
10:           "arr": [
11:             1,
12:             2,
13: -           3
13: +           3,
13: +           4
14:           ],
15:           "z": "zv"
16:         }
----

选项

$ jsondiff --help
jsondiff 0.1.0
A tool for outputs semantic difference of json

USAGE:
    jsondiff [FLAGS] [OPTIONS] <file-path1> <file-path2>

FLAGS:
    -h, --help                      Prints help information
    -n, --output-normalized-json    Outputs normalized json as "normalized1.json" and "normalized2.json"
    -V, --version                   Prints version information

OPTIONS:
    -U <unified>        Generate diffs with <n> lines of context [default: 3]

ARGS:
    <file-path1>    
    <file-path2>   

依赖项

~3–14MB
~127K SLoC