Skip to content

capturegraph.data.typed_json.type_handler #

TypeHandler #

Bases: ABC

Abstract base class for type-specific serialization handlers.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
class TypeHandler(ABC):
    """Abstract base class for type-specific serialization handlers."""

    # DECODING

    @abstractmethod
    def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
        """Return a dictionary of the required schema for decoding."""
        pass

    @abstractmethod
    def decode(self, obj: Dict[str, Any]) -> Any | None:
        """Decode the object to the target type."""
        pass

    def can_decode(self, obj: Dict[str, Any]) -> bool:
        """Check if the object can be decoded by this handler."""
        return all(
            isinstance(obj[key], value_types)
            for key, value_types in self.decodable_schema().items()
        )

    # ENCODING

    @abstractmethod
    def encodable_types(self) -> Tuple[type, ...]:
        """Return a tuple of the types that this handler can encode."""
        pass

    @abstractmethod
    def encode(self, obj: Any) -> Dict[str, Any] | None:
        """Encode the object to a JSON-serializable form."""
        pass

    def can_encode(self, obj: Any) -> bool:
        """Check if the object can be encoded by this handler."""
        return isinstance(obj, self.encodable_types())

decodable_schema() abstractmethod #

Return a dictionary of the required schema for decoding.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
@abstractmethod
def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
    """Return a dictionary of the required schema for decoding."""
    pass

decode(obj) abstractmethod #

Decode the object to the target type.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
@abstractmethod
def decode(self, obj: Dict[str, Any]) -> Any | None:
    """Decode the object to the target type."""
    pass

can_decode(obj) #

Check if the object can be decoded by this handler.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
def can_decode(self, obj: Dict[str, Any]) -> bool:
    """Check if the object can be decoded by this handler."""
    return all(
        isinstance(obj[key], value_types)
        for key, value_types in self.decodable_schema().items()
    )

encodable_types() abstractmethod #

Return a tuple of the types that this handler can encode.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
@abstractmethod
def encodable_types(self) -> Tuple[type, ...]:
    """Return a tuple of the types that this handler can encode."""
    pass

encode(obj) abstractmethod #

Encode the object to a JSON-serializable form.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
@abstractmethod
def encode(self, obj: Any) -> Dict[str, Any] | None:
    """Encode the object to a JSON-serializable form."""
    pass

can_encode(obj) #

Check if the object can be encoded by this handler.

Source code in capturegraph-lib/capturegraph/data/typed_json/type_handler.py
def can_encode(self, obj: Any) -> bool:
    """Check if the object can be encoded by this handler."""
    return isinstance(obj, self.encodable_types())