有没有办法在为json文件生成补丁时保留顺序?

时间:2015-04-02 13:54:21

标签: json validation getjson geojson json-patch

我是Json的新手,即JSON PATCH。     我有一个场景,我需要弄清楚相同对象的两个版本的Json文件,因为我使用的是json-patch-master。     但不幸的是,补丁生成的解释方式不同,即顺序不同,因此得到意外/无效的结果。     任何人都可以帮助我如何在生成Json补丁的同时保留订单吗?

**Here is the actual example.
Original Json file :**

[ {
  "name" : "name1",
  "roolNo" : "1"
}, {
  "name" : "name2",
  "roolNo" : "2"
}, {
  "name" : "name3",
  "roolNo" : "3"
}, {
  "name" : "name4",
  "roolNo" : "4"
} ]


**Modified/New Json file:  i.e. removed 2nd node of original file.**

[ {
  "name" : "name1",
  "roolNo" : "1"
}, {
  "name" : "name3",
  "roolNo" : "3"
}, {
  "name" : "name4",
  "roolNo" : "4"
} ]

**Patch/Diff Generated :**

[ {"op":"remove","path":"/3"},
{"op":"replace","path":"/1/name","value":"name3"},
{"op":"replace","path":"/1/roolNo","value":"3"},
{"op":"replace","path":"/2/name","value":"name4"},
{"op":"replace","path":"/2/roolNo","value":"4"}]

Very time I generate Diff/Patch it is giving different path/diff results.
And moreover the interpretation is different i.e. order is not preserving.

**Is there any way to get expected results i.e. [ {"op":"remove","path":"/1"} ] , in other words generated a patch/diff based some order so will get what is expected. ?
How to handle this kind of scenario ?**

Please help me.

Thank you so much.
~Shyam

1 个答案:

答案 0 :(得分:0)

我们目前正在Starcounter-Jack/JSON-Patch中处理此问题。

它似乎与原生Array.Observe - http://jsfiddle.net/tomalec/p4s7aw96/很好用。

试试Starcounter-Jack/JSON-Patch issues/65_ArrayObserve branch 我们会在垫片时将其作为新版本发布,并检查性能。

随时在JSON-Patch issue board

添加评论