2015-04-02 54 views
1

我是Json東西的新手,即JSON PATCH。 我有場景,我需要弄清楚兩個版本的Json文件的同一對象,因爲我使用的是json-patch-master。 但不幸的是,該補丁生成的解釋不同,即順序不同,因此得到意外/無效的結果。 任何人都可以幫助我如何保持秩序,同時生成JSON補丁?有沒有辦法在生成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

有趣的問題!我幫助維護的一個庫(https://github.com/Starcounter-Jack/JSON-Patch)也存在這個問題:http://jsfiddle.net/warpech/1epzuoj3/。我認爲它可以固定支持'Array.observe'的瀏覽器。但對於舊版瀏覽器,需要進行昂貴的髒檢查。 – warpech 2015-04-03 08:42:34

回答

0

我們目前正在研究這個問題在Starcounter-Jack/JSON-Patch

它似乎與本地工作很好Array.Observe - http://jsfiddle.net/tomalec/p4s7aw96/

嘗試Starcounter-Jack/JSON-Patch issues/65_ArrayObserve branch 我們將發佈它作爲新版本一次墊片和性能將被檢查。

隨時爲您添加評論在JSON-Patch issue board

+0

謝謝。可能我的問題有些含糊不清,讓我把它說得對。當我生成差異/補丁時,它顯示一個節點被刪除,並且還顯示其他一些節點正在被替換,實際上我只刪除了一個節點。發生這種情況的原因是補丁無法保留或維護節點的順序.....所以我需要一些方法讓補丁以某種順序生成diff,以便結果正確。 – user3252097 2015-04-06 07:48:56

+0

@ user3252097,這是目前正在進行中,請檢查http://jsfiddle.net/tomalec/p4s7aw96/回購在這裏https://github.com/tomalec/JSON-Patch/tree/issues/65_ArrayObserve_shim 不幸的是,它沒有能力使它與Shimmed'Array.observe'一起工作,所以它仍然需要本地支持。 – tomalec 2015-04-07 08:20:49

相關問題