json-patch / json-patch2

A possibile revision to the JSON-Patch format
44 stars 0 forks source link

Set operations #8

Open mnot opened 10 years ago

mnot commented 10 years ago

Feedback from IETF ALTO WG indicates it'd be good to be able to treat arrays as sets; e.g., "delete the set member with this value", "move this subset to another set".

briancavalier commented 10 years ago

@mnot I think this could be very useful, especially when dealing with typical RESTful crud APIs which are really sets most of the time anyway. The items are unique based on id, even if the API is returning a JSON array.

Using deep comparisons (like test requires) for set membership would probably be too expensive for a real implementation. The client and server would have to agree on what constitutes identity (an id field, a hash algorithm etc), but that's typical even without JSON Patch.

Seems like add and remove could be fairly simple, but maybe have to provide a hint that the target is a set. Perhaps something like using a - marker at the end of the path to indicate a set operation?

{ "op": "add", "path": "path/to/a/set/-", "value": <value, object, or array> },
{ "op": "remove", "path": "path/to/a/set/-", "value": <value, object, or array> }

Or maybe adding specific "set-add" and "set-remove" ops? I think I like the former (-) better, but not 100% sure what's best.

michaelkantor commented 9 years ago

"add-set", "remove-set" (or "union" and "complement") operations are critical to my current use case.

In an open environment where multiple companies are building clients that need to maintain data in sync, insuring that the current remove-by-index from an array performs as expected depends on everyone maintaining the same sort order for their data... a highly error prone restriction to enforce.

heruan commented 7 years ago

Set operations could be generalized as value-based operations as I have described in https://github.com/json-patch/json-patch2/issues/18.

HappyNomad commented 7 years ago

@heruan Sets don't have order whereas your "insert before/after certain existing values" suggestion depends on order.

@mnot Does "json-patch2" need the concept of order for collections (lists)? Simplicity is a stated goal, so can order in collections be completely omitted? I use JSON Patch only for persistent object models where all collections are sets of entities like what @briancavalier described. This seems to be a common scenario, so perhaps "json-patch2" could be geared specifically towards it.

mitar commented 4 years ago

I would prefer explicit set operations. But I think it is unclear how to define equality in a reasonable and general enough way.