Convert virtual-dom objects to and from JSON. Designed for generating virtual nodes on the server or in a web worker and then sending that to the client.
This lib can serialize both nodes and patches, but the patch JSON is a bit big due to the underlying
VirtualPatch structure. For a more efficient patch serialization algorithm, check out vdom-serialized-patch.
npm install vdom-as-json
If you need an AMD or browser-ready version, please use
dist/vdom-as-json.js when you
npm install, or download from wzrd.in. It will give you a global
var toJson = ; // convert node/patch to JSONvar fromJson = ; // rehydrate node/patch from JSON
Convert a virtual node to and from JSON
var h = ;var toJson = ;var fromJson = ;var node = ;// convert the node to jsonvar json = ;// re-hydrate the node from jsonvar rehydratedNode = ;
Convert a virtual patch to and from JSON
var h = ;var diff = ;var toJson = ;var fromJson = ;var node1 = ;var node2 = ;var patch = ;// convert the patch to jsonvar json = ;// re-hydrate the patch from jsonvar rehydratedPatch = ;
Stringify and parse
The API returns pure JSON objects. So if you need strings, then use
var h = ;var diff = ;var toJson = ;var fromJson = ;var node1 = ;var node2 = ;var patch = ;// convert the patch to a stringvar jsonString = JSON;// re-hydrate the patch from a stringvar rehydratedPatch = ;
var toJson = toJson;var fromJson = fromJson;
Using the standalone browser bundle (
var toJson = vdomAsJsontoJson;var fromJson = vdomAsJsonfromJson;
This library doesn't support thunks, hooks, etc., because it's not possible to serialize custom behavior.