How to generate json-crdt-patch by comparing two POJO JSON objects? #504
-
Hello The getting started section for json-joy/json-crdt shows a basic example where specific/targeted updates are made to a model, and finally a patch is generated using // Find "obj" object node at path [].
const obj = model.api.obj([]);
// Overwrite the "counter" last-write-wins register to 25.
// this targets a certain property of the model very specifically
obj.set({ counter: 25 });
// retrieve the JSON CRDT Patch operations which were applied to the model while we were editing it
const patch = model.api.flush(); I'm wondering how and if it is possible to generate an array of patch operations to go between two POJO JSON objects? Adding the same sample as in the above link here for clarity: var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
var diff = jsonpatch.compare(documentA, documentB);
//diff == [{op: "replace", path: "/user/lastName", value: "Collins"}] My initial though was to use the above compare method from Fast-JSON-Patch to get an array of patches, and to then try and build the crdt-patch operations using the PatchBuilder, but I don't feel like this is the best approach. The need for this arises from wanting to be able to change the model drastically and freely, but to then send the minimal amount of info across the network for replication. Thanks for any assistance and for the ton of work on these libraries! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There is already code which creates JSON CRDT Patch patches out of JSON Patch patches, see. import {JsonPatch} from 'json-joy/es2020/json-crdt/json-patch';
const patcher = new JsonPatch(model);
patcher.apply(yourJsonPatch); |
Beta Was this translation helpful? Give feedback.
There is already code which creates JSON CRDT Patch patches out of JSON Patch patches, see.