Skip to content

json

autocorpus.ac_bioc.json ¤

This module provides a custom JSON encoder for BioCCollection objects.

It includes: - BioCJSONEncoder: A subclass of JSONEncoder to handle BioC objects with a to_dict-like structure. - dump: Function to serialize BioCCollection to a JSON file-like object. - dumps: Function to serialize BioCCollection to a JSON-formatted string.

Classes¤

BioCJSON ¤

JSON serialization for BioC objects.

Functions¤
dump(obj, fp, **kwargs) staticmethod ¤

Serialize a BioCCollection object to a JSON file-like object.

Source code in autocorpus/ac_bioc/json.py
30
31
32
33
@staticmethod
def dump(obj: BioCCollection, fp, **kwargs) -> None:
    """Serialize a BioCCollection object to a JSON file-like object."""
    return json.dump(obj, fp, cls=BioCJSONEncoder, **kwargs)
dumps(obj, **kwargs) staticmethod ¤

Serialize a BioCCollection object to a JSON-formatted string.

Source code in autocorpus/ac_bioc/json.py
35
36
37
38
@staticmethod
def dumps(obj: BioCCollection, **kwargs) -> str:
    """Serialize a BioCCollection object to a JSON-formatted string."""
    return json.dumps(obj, cls=BioCJSONEncoder, **kwargs)
loads(json_str) staticmethod ¤

Deserialize a JSON-formatted string to a BioCCollection object.

Source code in autocorpus/ac_bioc/json.py
40
41
42
43
44
@staticmethod
def loads(json_str: str) -> BioCCollection:
    """Deserialize a JSON-formatted string to a BioCCollection object."""
    data = json.loads(json_str)
    return BioCCollection.from_json(data)

BioCJSONEncoder ¤

Bases: JSONEncoder

Custom JSON encoder for BioC-related objects.

Functions¤
default(o) ¤

Return a serializable object for JSON encoding, using to_dict if available.

Source code in autocorpus/ac_bioc/json.py
18
19
20
21
22
23
24
def default(self, o: Any) -> Any:
    """Return a serializable object for JSON encoding, using to_dict if available."""
    if hasattr(o, "to_dict") and callable(o.to_dict):
        return o.to_dict()
    if o is None:
        return None
    return super().default(o)