Skip to content

json

autocorpus.ac_bioc.bioctable.json ¤

This module provides a custom JSON encoder for BioCTableCollection objects.

Classes¤

BioCTableJSON ¤

Utility class for serializing BioCTableCollection objects to JSON.

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

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

Parameters:

Name Type Description Default
obj

object The object to serialize.

required
fp

file-like object The file-like object to write the JSON data to.

required
**kwargs

dict Additional keyword arguments to pass to json.dump.

{}
Source code in autocorpus/ac_bioc/bioctable/json.py
82
83
84
85
86
87
88
89
90
91
92
93
94
@staticmethod
def dump(obj, fp, **kwargs):
    """Serialize a BioCTableCollection object to a JSON file-like object.

    Parameters:
        obj : object
            The object to serialize.
        fp : file-like object
            The file-like object to write the JSON data to.
        **kwargs : dict
            Additional keyword arguments to pass to `json.dump`.
    """
    return json.dump(obj, fp, cls=BioCTableJSONEncoder, **kwargs)
dumps(obj, **kwargs) staticmethod ¤

Serialize a BioCTableCollection object to a JSON-formatted string.

Parameters:

Name Type Description Default
obj

object The object to serialize.

required
**kwargs

dict Additional keyword arguments to pass to json.dumps.

{}

Returns:

Type Description

str A JSON-formatted string representation of the object.

Source code in autocorpus/ac_bioc/bioctable/json.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@staticmethod
def dumps(obj, **kwargs):
    """Serialize a BioCTableCollection object to a JSON-formatted string.

    Parameters:
        obj : object
            The object to serialize.
        **kwargs : dict
            Additional keyword arguments to pass to `json.dumps`.

    Returns:
        str
            A JSON-formatted string representation of the object.
    """
    return json.dumps(obj, cls=BioCTableJSONEncoder, **kwargs)

BioCTableJSONEncoder ¤

Bases: BioCJSONEncoder

Custom JSON encoder for objects with a to_dict method.

This encoder extends the BioCJsonEncoder to handle objects that implement a to_dict method, converting them to a dictionary representation for JSON serialization.

Functions¤
default(o) ¤

Convert an object to a JSON-serializable dictionary.

Parameters:

Name Type Description Default
o

object The object to serialize.

required

Returns:

Type Description

dict A dictionary representation of the object if it is serializable.

Source code in autocorpus/ac_bioc/bioctable/json.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def default(self, o):
    """Convert an object to a JSON-serializable dictionary.

    Parameters:
        o : object
            The object to serialize.

    Returns:
        dict
            A dictionary representation of the object if it is serializable.
    """
    match o:
        case BioCTableCell():
            return {
                "cell_id": o.cell_id,
                "cell_text": o.cell_text,
            }
        case BioCTablePassage():
            return {
                "offset": o.offset,
                "infons": o.infons,
                "text": o.text,
                "column_headings": [self.default(c) for c in o.column_headings],
                "data_section": [
                    {
                        "table_section_title_1": section.get(
                            "table_section_title_1", ""
                        ),
                        "data_rows": [
                            [self.default(cell) for cell in row]
                            for row in section.get("data_rows", [])
                        ],
                    }
                    for section in o.data_section
                ],
                "annotations": [self.default(a) for a in o.annotations],
                "relations": [self.default(r) for r in o.relations],
            }
        case BioCTableDocument():
            return {
                "id": o.id,
                "inputfile": o.inputfile,
                "infons": o.infons,
                "passages": [self.default(p) for p in o.passages],
                "annotations": [self.default(a) for a in o.annotations],
                "relations": [self.default(r) for r in o.relations],
            }
        case BioCTableCollection():
            return {
                "source": o.source,
                "date": o.date,
                "key": o.key,
                "infons": o.infons,
                "documents": [self.default(d) for d in o.documents],
            }
        case _:
            # Let the base class default method raise the TypeError
            return super().default(o)