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.82k stars 215 forks source link

JsonPatch.Compare() not working properly. #202

Closed hafeefshaik closed 6 years ago

hafeefshaik commented 6 years ago

Original object :

{ "id": "06449690-3e21-43c9-0c13-08d5a8f9a4ff", "firstName": "Hafeef", "lastName": "Shaik", "roles": [ "Developer", "Admin" ] }

Later added couple of other roles. Modified object:

{ "id": "06449690-3e21-43c9-0c13-08d5a8f9a4ff", "firstName": "Hafeef", "lastName": "Shaik", "roles": [ "Admin", "Business Analyst", "Delivery Manager", "Developer" ] }

When I compare, I could see couple of replace action as mentioned below.

[ { "op": "replace", "path": "/roles/1", "value": "Business Analyst" }, { "op": "replace", "path": "/roles/0", "value": "Admin" }, { "op": "add", "path": "/roles/2", "value": "Delivery Manager" }, { "op": "add", "path": "/roles/3", "value": "Developer" } ]

Please correct me if I am wrong, What I believe there should be two add operation as I am adding two new roles to the object.

alshakero commented 6 years ago

Hi, yes this happens when you add to the start of the array. compare is working properly but not in the most optimized manner.

If you want to avoid this, please add to the end of the array, use push not unshift.

hafeefshaik commented 6 years ago

@alshakero as I am using angular 5 two way binding, I think it's bit hard to handle it. Is there any other work around?

alshakero commented 6 years ago

I don't think so. If you compare the arrays manually:

"roles": [
"Developer",
"Admin"
]

and

"roles": [
"Admin",
"Business Analyst",
"Delivery Manager",
"Developer"
]

How would you explain the difference?

alshakero commented 6 years ago

Closing for lack of response.