Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.81k stars 215 forks source link

can I use JSON-Patch for array this way? #68

Closed beetaa closed 9 years ago

beetaa commented 9 years ago

assume that there's an object like:

var obj_src = {
  name: "jone",
  city: "Sydney",
  pets: [
    {nickname: "volo", category: "dog", age: "3"},
    {nickname: "wabit", category: "cat", age: "5"},
    {nickname: "moaoaa", category: "tiger", age: "1"},
    {nickname: "gofast", category: "lion", age: "3"}
  ]
}

if pets' nickname is unique, can I use patch like {op: "replace", path: "/pets[nickname=wabit]/age", value: "9"} to alter wabit's age instead using {op: "replace", path: "/pets/1/age", value: "9"} because sometimes I don't know the array order.

Starcounter-Jack commented 9 years ago

No. The JSON Patch behavior is standardized in RFC6902, so I'm afraid we cannot change the way replace patches work.

Sent from my iPhone

On 23 Mar 2015, at 11:14, beetaa notifications@github.com wrote:

assume that there's an object like:

var obj_src = { name: "jone", city: "Sydney", pets: [ {nickname: "volo", category: "dog", age: "3"}, {nickname: "wabit", category: "cat", age: "5"}, {nickname: "moaoaa", category: "tiger", age: "1"}, {nickname: "gofast", category: "lion", age: "3"} ] } if pets' nickname is unique, can I use patch like {op: "replace", path: "/pets[nickname=wabit]/age", value: "9"} to alter wabit's age instead using {op: "replace", path: "/pets/1/age", value: "9"} because sometimes I don't know the array order.

— Reply to this email directly or view it on GitHub.

beetaa commented 9 years ago

Thank you Jack, but how about extending RFC6902 some more features? just call RFC6902-x

tomalec commented 9 years ago

I believe we should serve tiny lib that do one job, and do it well and blazing fast ;) With tiny support of jsonpointer #66, and native Array.prototype.filter I would suggest something like

var patch = [{op: "replace", path: "/age", value: "9"}];
var cssFilter = function(selector){
 return function(el, index){
   // do some nice CSS selector style filtering, or anything you want
 }
}
jsonpatch.apply( jsonpointer( obj_src, "/pets").filter(cssFilter("[nickname=wabit]")), patch);

Or write completely separated concept of JSON-CSS-Pointer (extended https://tools.ietf.org/html/rfc6901), and then use it like

var cssPatch = [{op: "replace", path: "/pets[nickname=wabit]/age", value: "9"}];
jsonpatch.apply( obj_src,  jsonCSSSelectorToPointer( obj_src, cssPatch ));
beetaa commented 9 years ago

wow! nice answer, @tomalec , thank you very much! I completely agree with your points.

tomalec commented 9 years ago

@beetaa Could we close this issue?

Feel free to vote, and comment at #66.