Skip to content

capturegraph.procedures.exporting.serializer #

Procedure Serialization#

This module handles the serialization of procedure graphs to JSON format for execution by the app. The serializer converts the Python procedure DAG into a structured JSON representation that the mobile app can parse and execute.

procedure_to_dict(procedure) #

Serializes a Procedure DAG into a dictionary representation.

The procedure graph is flattened into nodes with unique IDs, capturing their kinds, return types, labels, settings, inputs, and substeps. The resulting dictionary can be easily converted to JSON for transmission to the app.

Parameters:

Name Type Description Default
procedure Procedure[PVoid]

The root Procedure node to serialize

required

Returns:

Type Description
dict[str, Any]

A dictionary representing the serialized procedure graph

Source code in capturegraph-lib/capturegraph/procedures/exporting/serializer.py
def procedure_to_dict(procedure: Procedure[PVoid]) -> dict[str, Any]:
    """
    Serializes a Procedure DAG into a dictionary representation.

    The procedure graph is flattened into nodes with unique IDs, capturing
    their kinds, return types, labels, settings, inputs, and substeps. The
    resulting dictionary can be easily converted to JSON for transmission
    to the app.

    Args:
        procedure (Procedure[PVoid]): The root Procedure node to serialize

    Returns:
        A dictionary representing the serialized procedure graph
    """

    nodes: dict[str, dict[str, Any]] = {}

    def flatten(procedure: Procedure[PType]) -> str:
        if procedure._uuid not in nodes:
            node = {
                "kind": procedure._kind.__name__,
                "return_type": procedure._return_type.__name__,
                "label": procedure.label,
                "settings": procedure._settings,
                "inputs": {
                    name: flatten(value) for name, value in procedure._inputs.items()
                },
                "substeps": [flatten(substep) for substep in procedure._substeps],
            }

            for key in list(node.keys()):
                if not node[key]:
                    del node[key]

            nodes[procedure._uuid] = node

        return procedure._uuid

    procedure_dictionary = {
        "root": flatten(procedure),
        "nodes": nodes,
    }

    uuid_map: dict[str, str] = {}

    hex_length = len(f"{len(nodes) - 1:X}")
    for index, (uuid, node) in enumerate(nodes.items()):
        uuid_map[uuid] = f"{index:X}".zfill(hex_length)

    return _replace_strings_in_dict(procedure_dictionary, uuid_map)

procedure_to_json(procedure, indent=4, **json_kwargs) #

Serializes a Procedure DAG into a JSON string representation.

This function converts the procedure graph into a structured JSON format that can be transmitted to the app for execution.

Parameters:

Name Type Description Default
procedure Procedure[PVoid]

The root Procedure node to serialize

required
indent int

Number of spaces for JSON indentation (default: 4)

4
**json_kwargs Any

Additional keyword arguments for json.dumps

{}

Returns:

Type Description
str

A JSON string representing the serialized procedure graph

Source code in capturegraph-lib/capturegraph/procedures/exporting/serializer.py
def procedure_to_json(
    procedure: Procedure[PVoid],
    indent: int = 4,
    **json_kwargs: Any,
) -> str:
    """
    Serializes a Procedure DAG into a JSON string representation.

    This function converts the procedure graph into a structured JSON format
    that can be transmitted to the app for execution.

    Args:
        procedure (Procedure[PVoid]): The root Procedure node to serialize
        indent (int): Number of spaces for JSON indentation (default: 4)
        **json_kwargs (Any): Additional keyword arguments for json.dumps

    Returns:
        A JSON string representing the serialized procedure graph
    """

    return json.dumps(procedure_to_dict(procedure), indent=indent, **json_kwargs)