简体   繁体   English

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

[英]JSON patch rfc6902: Sequential operation and index

In JSON patch, how should one use the index for subsequent operation on the same array. 在JSON补丁中,如何在同一个数组上使用索引进行后续操作。 For example, consider 例如,考虑一下

 var source = { colors: ['Red', 'Green', 'Blue'] };
 var target = { colors: [] }; 

patch doc (operations) 补丁文档(操作)

[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/1"},
{"op":"remove","path":"/colors/2"}]

If I consider the index of the source, the above indexes are correct. 如果我考虑源的索引,上面的索引是正确的。 When I apply it sequentially though, the index are incorrect. 当我按顺序应用它时,索引不正确。 That is if I remove, 0 th and 1st index, there is no element at index 2. 也就是说,如果我删除第0个和第1个索引,则索引2处没有元素。

I could think of couple of ways to handle this. 我可以想到几种方法来处理这个问题。 Either group all delete operation on an array, then keep a temporary structure to hold/manipulate change in index during deletion. 对数组进行所有删除操作的组,然后在删除期间保留一个临时结构来保存/操作索引中的更改。 Or, keep the index relative to a mutating value 或者,保持索引相对于变异值

[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"}]

it makes sense if the operation is considered mutation of a resource in sequence. 如果操作被认为是顺序资源的变异,那么这是有意义的。

Is there any standard on this. 对此有什么标准吗? I cannot see anything about it in the spec. 我在规范中看不到任何相关内容。 A.4. A.4。 Removing an Array Element 删除数组元素

Is there any standard on this 对此有什么标准吗?

The section about the evaluation of the operations seems to be quite clear: 关于操作评估部分似乎很清楚:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM