简体   繁体   中英

JSON patch rfc6902: Sequential operation and index

In JSON patch, how should one use the index for subsequent operation on the same array. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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