Skip to content

medcat.storage.jsonserialiser

Classes:

Attributes:

  • T

T module-attribute

T = TypeVar('T')

DataClassHandler

Bases: TypeHandler[T]

Methods:

Attributes:

type_cls class-attribute instance-attribute

type_cls = type(dataclass)

type_name class-attribute instance-attribute

type_name = 'dataclass'

decode

decode(obj: Any) -> T
Source code in medcat-v2/medcat/storage/jsonserialiser.py
130
131
132
133
134
135
136
137
138
def decode(self, obj: Any) -> T:
    cls_path: str = obj[self._cls_key]
    module_name, cls_name = cls_path.rsplit('.', 1)
    module = importlib.import_module(module_name)
    cls = getattr(module, cls_name)
    if not is_dataclass(cls):
        raise TypeError(f"Unsupported type: {cls}")
    # NOTE: mypy doesn't know that the dataclass init can be called here
    return cast(T, cls(**obj[self._data_key]))  # type: ignore

encode

encode(obj: T) -> Any
Source code in medcat-v2/medcat/storage/jsonserialiser.py
122
123
124
125
126
127
128
def encode(self, obj: T) -> Any:
    cls_path = f"{obj.__class__.__module__}.{obj.__class__.__name__}"
    return {
        self._cls_key: cls_path,
        # NOTE: mypy doesn't know that this is a dataclass
        self._data_key: asdict(obj),  # type: ignore
    }

should_encode

should_encode(obj: Any) -> bool
Source code in medcat-v2/medcat/storage/jsonserialiser.py
119
120
def should_encode(self, obj: Any) -> bool:
    return is_dataclass(obj)

DateTimeHandler

Bases: TypeBasedHandler[datetime]

Methods:

Attributes:

type_cls class-attribute instance-attribute

type_cls = datetime

type_name class-attribute instance-attribute

type_name = 'datetime'

decode

decode(obj: Any)
Source code in medcat-v2/medcat/storage/jsonserialiser.py
109
110
def decode(self, obj: Any):
    return datetime.fromisoformat(obj)

encode

encode(obj: datetime) -> Any
Source code in medcat-v2/medcat/storage/jsonserialiser.py
106
107
def encode(self, obj: datetime) -> Any:
    return obj.isoformat()

JsonSerialiser

Bases: Serialiser

Methods:

Attributes:

ser_type class-attribute instance-attribute

ser_type = json

deserialise

deserialise(target_file: str) -> dict[str, Any]
Source code in medcat-v2/medcat/storage/jsonserialiser.py
157
158
159
def deserialise(self, target_file: str) -> dict[str, Any]:
    with open(target_file, 'r') as f:
        return json.load(f, object_hook=_def_registry.decode)

serialise

serialise(raw_parts: dict[str, Any], target_file: str) -> None
Source code in medcat-v2/medcat/storage/jsonserialiser.py
153
154
155
def serialise(self, raw_parts: dict[str, Any], target_file: str) -> None:
    with open(target_file, 'w') as f:
        json.dump(raw_parts, f, default=_def_registry.encode)

NumpyArrayHandler

Bases: TypeBasedHandler[ndarray]

Methods:

  • decode

    Decode to numpy ndarray.

  • encode

    Encode numpy ndarray.

Attributes:

type_cls class-attribute instance-attribute

type_cls = ndarray

type_name class-attribute instance-attribute

type_name = 'ndarray'

decode

decode(obj: Any) -> ndarray

Decode to numpy ndarray.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
81
82
83
84
85
86
def decode(self, obj: Any) -> np.ndarray:
    """Decode to numpy ndarray."""
    return np.array(
        obj[self._data_key],
        dtype=obj[self._dtype_key]).reshape(
            obj[self._shape_key])

encode

encode(obj: ndarray) -> Any

Encode numpy ndarray.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
74
75
76
77
78
79
def encode(self, obj: np.ndarray) -> Any:
    """Encode numpy ndarray."""
    return {
        self._data_key: obj.tolist(),
        self._dtype_key: str(obj.dtype),
        self._shape_key: obj.shape}

SetHandler

Bases: TypeBasedHandler[set]

Methods:

Attributes:

type_cls class-attribute instance-attribute

type_cls = set

type_name class-attribute instance-attribute

type_name = 'set'

decode

decode(obj: Any) -> set

Decode to set.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
97
98
99
def decode(self, obj: Any) -> set:
    """Decode to set."""
    return set(obj)

encode

encode(obj: set) -> Any

Encode set.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
93
94
95
def encode(self, obj: set) -> Any:
    """Encode set."""
    return list(obj)

TypeBasedHandler

Bases: TypeHandler[T]

Methods:

should_encode

should_encode(obj: Any) -> bool
Source code in medcat-v2/medcat/storage/jsonserialiser.py
63
64
def should_encode(self, obj: Any) -> bool:
    return isinstance(obj, self.type_cls)

TypeHandler

Bases: Protocol, Generic[T]

Methods:

Attributes:

type_cls instance-attribute

type_cls: type

type_name instance-attribute

type_name: str

decode

decode(obj: Any) -> T

Decode an object of the registered type.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
27
28
29
def decode(self, obj: Any) -> T:
    """Decode an object of the registered type."""
    pass

encode

encode(obj: T) -> Any

Encode an object of the registered type.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
23
24
25
def encode(self, obj: T) -> Any:
    """Encode an object of the registered type."""
    pass

should_encode

should_encode(obj: Any) -> bool
Source code in medcat-v2/medcat/storage/jsonserialiser.py
20
21
def should_encode(self, obj: Any) -> bool:
    pass

TypeRegistry

TypeRegistry()

Methods:

  • decode

    Decode an object using the registered handler.

  • encode

    Encode an object using the registered handler.

  • register

    Register a new type handler.

Attributes:

Source code in medcat-v2/medcat/storage/jsonserialiser.py
37
38
def __init__(self) -> None:
    self.handlers: dict[str, TypeHandler] = {}

handlers instance-attribute

handlers: dict[str, TypeHandler] = {}

decode

decode(obj: Any) -> Any

Decode an object using the registered handler.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
52
53
54
55
56
57
58
def decode(self, obj: Any) -> Any:
    """Decode an object using the registered handler."""
    if isinstance(obj, dict) and self._type_key in obj:
        type_name = obj[self._type_key]
        if type_name in self.handlers:
            return self.handlers[type_name].decode(obj[self._data_key])
    return obj  # Default handling

encode

encode(obj: Any) -> Any

Encode an object using the registered handler.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
44
45
46
47
48
49
50
def encode(self, obj: Any) -> Any:
    """Encode an object using the registered handler."""
    for handler in self.handlers.values():
        if handler.should_encode(obj):
            return {self._type_key: handler.type_name,
                    self._data_key: handler.encode(obj)}
    return obj  # Default handling

register

register(handler: TypeHandler) -> None

Register a new type handler.

Source code in medcat-v2/medcat/storage/jsonserialiser.py
40
41
42
def register(self, handler: TypeHandler) -> None:
    """Register a new type handler."""
    self.handlers[handler.type_name] = handler