-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deep-object-merge ? #73
Comments
Hi @Filipeue I'm not sure I'm following exactly but if you were to take the difference of two objects (lhs and rhs) and merge the diff to the lhs object - would you not just end up with the rhs object? |
@mattphillips yeah that's exactly what I am trying to achieve |
Sorry if I'm missing something but could you not store the I'm not sure what value |
@mattphillips But as you are interested in the actual problem I am trying to solve: These questions may come up:
|
Hey @mattphillips did you find a solution to this? I'm looking for a similar way to merge the left object with the diff and get the right object back, sometime after the initial diff was generated :) |
If you need to track changes on JSON data, consider the JSON Patch mechanism. I started to use this for history edits, only storing the patches and using this lib to apply / revert changes to the data :) |
Is this a duplicate of #71 ? |
imagine the original is a really big dynamically created json object. now the target device is ready to receive a new state.. it could tell the source: "ey.. I am at version X, now gimme the diff to the current version" obviously for this, the last sent state must be known to both locations but that's trivial. I hope this clears up what OP wanted to ask about. @lmaccherone already posted a nice little solution to this that works really well as following: // you receive a new state.. so you save the last processed state as old state
oldState = state;
// then you json parse the incoming new state, that was generated on the other end as following:
// JSON.stringify(diff(oldState, state))
// then you apply the diff to the old state:
state = applyDiff(oldState, JSON.parse(text)); voila, state is now equal to upstream without sending the whole json down the wire.. |
Nice explanation @GottZ and thanks for the shout out! I ended up publishing this as its own npm package: https://www.npmjs.com/package/@transformation-dev/deep-object-diff-apply. |
already using it in production for a custom monitoring solution since yesterday ;) bit OT: So.. I'm diffing the monitoring state, wrap this in a e2e openpgp message, and send it off to a external device, that then patches it back together and shows a custom monitoring for some DMZ resources.. |
hm. the diffs are not really serializable and deserializable with JSON.parse and stringify without custom logic. |
If you want something simple and lightweight, I can recommend @ungap/structured-clone. However, if you want fast and based upon an international standard, there is cbor-x with the structured cloning extension. Either one of those handles undefined as well as all but a few JavaScript types. |
I was already thinking about switching to bson, protobuf or a custom hacky (time budgeted) de/serializer but this is nice indeed! edit: |
Is there a suggested solution how to get new object merging original object with diff object created with deep-object-diff?
I intend to store original object + diff, so later I can merge those two to get new object. Then I compare original with new object using some other lib and render diff on a page.
The text was updated successfully, but these errors were encountered: