objectDiff.js
objectDiff.js copied to clipboard
Use JSONPatch format
http://jsonpatch.com/
JSONPatch has two oddities I can see:
- The JSON Pointer format uses weird escape codes, I assume because doing that saves a couple bytes here and there
- The remove item can't specify a count, so if you remove a big sequence from an array, you get a ton of little removal events (strange since this totally negates the bytes saved by JSON Pointer)
This is a format I would recommend instead:
-
type
- Either"set"
,"add"
, or"rm"
-
path
- An array representing the path from the root object. For example,["a",2,"b]
representsvalueA.a[2].b
. Will be an empty array if the change applies to the top-level object (ievalueA
directly). -
val
- The value the indicated property was changed to. Only defined for the"set"
type. -
index
- The index at which an item was added or removed from an array. Only defined for the"add"
and"rm"
types. -
vals
- An array of values added into the indicated property. Only defined for the"add"
type. -
num
- The number of items removed from the indicated property. Only defined for the"remove"
type.
Yes its not an RFC standard.. but its better. When looping through that kind of output, you can easily check to see if a path applies to whatever you're doing, because you can check every part of the path (maybe you only care about result.path.slice(0,2)
for example). With JSONPatch you need to remember to apply some de-escape-code function to get the path in a usable format (unless you know for sure you don't currently and never will have keys with ~ or / in them).