Skip to content

serialize

serialize #

Type ↔ JSON schema, and structural equality between types.

STRUCTURAL_TAGS = frozenset({'struct', 'array', 'map', 'path', 'void'}) module-attribute #

The wire tags whose shape is spelled out inline; every other tag names a scalar.

from_schema(obj) #

Rebuild a type from its wire form.

Composites recurse; a scalar resolves to its registered type, or to an opaque scalar carrying the document's kind/extensions when this process does not implement the name.

Source code in capturegraph-lib/capturegraph/types/schema/serialize.py
def from_schema(obj: dict[str, Any]) -> type[CGType]:
    """Rebuild a type from its wire form.

    Composites recurse; a scalar resolves to its registered type, or to an
    opaque scalar carrying the document's ``kind``/``extensions`` when this
    process does not implement the name.
    """
    type_name = obj.get("type")
    if not isinstance(type_name, str) or not type_name:
        raise ValueError(f"Type JSON needs a non-empty 'type' string: {obj!r}")
    match type_name:
        case "array":
            return Array[from_schema(obj["element"])]
        case "struct":
            fields = {n: from_schema(f) for n, f in obj["fields"].items()}
            return type("Struct", (Struct,), {"__annotations__": fields})
        case "map":
            return Map[from_schema(obj["key"]), from_schema(obj["element"])]
        case "path":
            target = obj.get("target")
            return Path[from_schema(target)] if target is not None else Path
        case "void":
            return Void
        case _:
            known = scalar_for(type_name)
            if known is not None:
                return known
            kind = obj.get("kind")
            if kind not in ("json", "binary", None):
                raise ValueError(f"Scalar {type_name!r} has unknown kind {kind!r}")
            extensions = tuple(obj.get("extensions", ()))
            if kind == "json" and not extensions:
                extensions = ("json",)
            return opaque_scalar(type_name, kind, extensions)

schema(cgtype) #

The wire form (schema) of a type.

Source code in capturegraph-lib/capturegraph/types/schema/serialize.py
def schema(cgtype: type[CGType]) -> dict[str, Any]:
    """The wire form (schema) of a type."""
    return cgtype.schema()

structural_eq(a, b) #

Whether two types describe the same on-disk structure; scalar extensions are ignored.

Source code in capturegraph-lib/capturegraph/types/schema/serialize.py
def structural_eq(a: type[CGType], b: type[CGType]) -> bool:
    """Whether two types describe the same on-disk structure; scalar ``extensions`` are ignored."""
    return a is b or _identity(schema(a)) == _identity(schema(b))