Skip to content

graflo.architecture.graph_types.identifiers

Graph identifier aliases and edge-key serialization.

deserialize_edge_key(key)

Deserialize a JSON-array edge key back to an edge id tuple.

Source code in graflo/architecture/graph_types/identifiers.py
def deserialize_edge_key(key: str) -> tuple[str, str, str | None]:
    """Deserialize a JSON-array edge key back to an edge id tuple."""
    try:
        parsed = json.loads(key)
    except json.JSONDecodeError as exc:
        raise ValueError(f"Invalid edge key JSON: {key!r}") from exc
    if not isinstance(parsed, list) or len(parsed) != 3:
        raise ValueError(f"Invalid edge key JSON array: {key!r}")
    if not all(isinstance(part, str) for part in parsed[:2]):
        raise ValueError(f"Invalid edge key JSON array: {key!r}")
    relation_part = parsed[2]
    if relation_part is not None and not isinstance(relation_part, str):
        raise ValueError(f"Invalid edge key JSON array: {key!r}")
    relation = relation_part if relation_part else None
    return (parsed[0], parsed[1], relation)

serialize_edge_key(edge_id)

Serialize an edge id tuple to a JSON-safe string key.

Uses a JSON array [source, target, relation] so vertex or relation names may contain | or other special characters without ambiguity.

Source code in graflo/architecture/graph_types/identifiers.py
def serialize_edge_key(edge_id: tuple[str, str, str | None]) -> str:
    """Serialize an edge id tuple to a JSON-safe string key.

    Uses a JSON array ``[source, target, relation]`` so vertex or relation names
    may contain ``|`` or other special characters without ambiguity.
    """
    source, target, relation = edge_id
    return json.dumps([source, target, relation], separators=(",", ":"))