json-patch / json-patch2

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

Batches from a base path to decrease the amount of text required. #9

Open disordinary opened 9 years ago

disordinary commented 9 years ago

Having the paths in ever operation seems needlessly verbose for batches of updates, so I propose the introduction of batches.

If I had an object:

    {
        id,
        name,
        password,
        email,
        phone
    }

And I wanted to replace the email and phone items I would need to go:

    {"op": "replace", "path": "/user/0/email", "value": "new@email.address"}

    {"op": "replace", "path": "/user/0/phone", "value": "+64 4 555 5555"}

If I have big objects with lots of changes then that is going to be allot of information to transport, something like:

    {"op": "replace", "path": "/user/0/", "patch": 
            { "email": "new@email.address", "phone": "+64 4 555 5555"}
    }
mkantor commented 9 years ago

How about a general-purpose scope operation whose value is an arbitrary JSON patch array?

[
  { "op": "scope", "path": "/user/0", "value": [
    { "op": "replace", "path": "/email", "value": "new@email.address" },
    { "op": "replace", "path": "/phone", "value": "+64 4 555 5555" },
    { "op": "remove", "path": "/password" }
  ] },
  { "op": "add", "path": "/hello", "value": ["world"] }
]
disordinary commented 9 years ago

Yes that's a good idea, and you could have scopes within scopes. In this case the paths within the scopes should be relative. The leading / would indicate that they would go back to the root of the object, so they should start with a ./ or nothing, i.e. ./email, ./phone or email, phone, you could theoretically also have ../1/email to modify something outside of the path, in that case modify /user/1/ instead of /user/0/ but that is getting silly.

[
  { "op": "scope", "path": "/user/0", "value": [
    { "op": "replace", "path": "email", "value": "new@email.address" },
    { "op": "replace", "path": "phone/office", "value": "+64 4 555 5555" },
    { "op": "remove", "path": "password" }
  ] },
  { "op": "add", "path": "/hello", "value": ["world"] }
]

It's still too much duplicated text and information for my liking. I'm thinking if you are storing millions of patches or sending thousands of patches down the wire, but if you are clever with nested scopes it is much more efficient than having every operation separate.

mitar commented 4 years ago

I would be against this proposal because I think this is premature optimization. These days it is easy to use some compression algorithm on top of the transport you are using for your JSON patches, which would make deduplicate such repetitions. I would not complicate the PATCH format itself.