JSON补丁rfc6902:顺序操作和索引

JSON patch rfc6902: Sequential operation and index

本文关键字:索引 顺序操作 补丁 rfc6902 JSON      更新时间:2023-09-26

在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中看不到任何关于它的内容。删除阵列元素

这个有什么标准吗

关于作战评估的部分似乎很清楚:

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.