set custom hydration for node data
In the simpliest case, this can be used to cast the data types appropriately (or alternatively you could just cast the Graph itself.
const grf: Graph<number> = ...
const builder = graphJson().nodeDatum(data => data as number);
const deser: MutGraph<number> = builder(JSON.parse(JSON.serialize(grf)));
Ideally though, you'll use typescripts warnings to make serialization error proof:
const grf: Graph<number> = ...
const builder = graphJson().nodeDatum(data => {
if (typeof data === "number") {
return data;
} else {
throw new Error("...");
}
});
const deser: MutGraph<number> = builder(JSON.parse(JSON.serialize(grf)));
get the node data hydrator
an operator that hydrates serialized json into a graph
Since type information is inherently lost with serialization, use nodeDatum and linkDatum to define node and link data type, and optionally perform extra deserialization.