在JSON补丁中,如何在同一个数组上使用索引进行后续操作。例如,考虑
var source = { colors: ['Red', 'Green', 'Blue'] };
var target = { colors: [] };
补丁文档(操作)
[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/1"},
{"op":"remove","path":"/colors/2"}]
如果我考虑源的索引,则上述索引是正确的。当我按顺序应用它时,索引不正确。也就是说,如果我删除第0个和第1个索引,则索引2处没有元素。
我可以想到几种方法来处理这个问题。对数组进行所有删除操作的组,然后在删除期间保留一个临时结构来保存/操作索引中的更改。或者,保持索引相对于变异值
[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"}]
如果将操作视为序列中的资源变异,则有意义。
这是否有任何标准。我在规范中看不到任何相关内容。 A.4. Removing an Array Element
答案 0 :(得分:4)
这个
有什么标准吗?
section about the evaluation of the operations似乎很清楚:
Evaluation of a JSON Patch document begins against a target JSON
document. Operations are applied sequentially in the order they
appear in the array. Each operation in the sequence is applied to
the target document; the resulting document becomes the target of the
next operation.