Function graphJson

  • create a Json graph constructor with default settings

    If you serialize a graph or node using JSON.stringify, this operator can be used to hydrate them back into a MutGraph. This is expecially useful for serializing the graph to do layouts in a separate worker.

    If you serialize a GraphNode the deserialized graph will be the same node that was serialized, but the rest of that node's connected component will be deserialized as well, and still reachable via Graph#nodes. The type information will inherently be lost, and will need to be cast.

    Since serialization doesn't inherently imply deserialization, Json#nodeDatum and Json#linkDatum can be used to provide custom hydration for node and linke data.

    Parameters

    • Rest ...args: readonly never[]

    Returns DefaultJson

    Example

    This example shows how to deserialize a graph while losing type information.

    const orig: Graph = ...
    const serialized = JSON.stringify(orig);
    const builder = graphJson();
    const deserialized = builder(JSON.parse(serialized));

    Note, that because no custom hydrators were given, deserialized will have type MutGraph<unknown, unknown> which probably isn't desired.

    Example

    This example demonstrates using a simple data hydrator.

    const orig: Graph<number, string> = ...
    const serialized = JSON.stringify(orig);
    const builder = graphJson()
    .nodeDatum(data => data as number)
    .linkDatum(data => data as string);
    const deserialized = builder(JSON.parse(serialized));

    Now deserialized has the same type as the original grah. In real use, you'll probably want to use type guards to guarantee the data was deserialized correctly.

Generated using TypeDoc