vdom-as-json
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.
Install
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 vdomAsJson
object.
Usage
var toJson = ; // convert node/patch to JSON var fromJson = ; // rehydrate node/patch from JSON
Examples
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 JSON.parse()
and JSON.stringify()
:
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 = ;
Standalone
Using browserify/webpack:
var toJson = toJson;var fromJson = fromJson;
Using the standalone browser bundle (dist/vdom-as-json.js
):
var toJson = vdomAsJsontoJson;var fromJson = vdomAsJsonfromJson;
Limitations
This library doesn't support thunks, hooks, etc., because it's not possible to serialize custom behavior.