Skip to content

Serialize

dumps

dumps(
    o: Any, use_decimal: bool = True, **json_kwargs
) -> str

Serialize object to string

Parameters:

Name Type Description Default
o Any

the object to serialize

required
use_decimal bool

use decimal.Decimal for numbers. Defaults to True.

True
**json_kwargs

other arguments passed to json.dumps

{}

Returns:

Name Type Description
str str

the serialized object as a string

Source code in .venv/lib/python3.12/site-packages/bloqade/analog/serialize.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@beartype
def dumps(
    o: Any,
    use_decimal: bool = True,
    **json_kwargs,
) -> str:
    """Serialize object to string

    Args:
        o (Any): the object to serialize
        use_decimal (bool, optional): use decimal.Decimal for numbers. Defaults to True.
        **json_kwargs: other arguments passed to json.dumps

    Returns:
        str: the serialized object as a string
    """
    if not isinstance(o, Serializer.types):
        raise TypeError(
            f"Object of type {type(o)} is not JSON serializable. "
            f"Only {Serializer.types} are supported."
        )
    return json.dumps(o, cls=Serializer, use_decimal=use_decimal, **json_kwargs)

load

load(
    fp: Union[TextIO, str],
    use_decimal: bool = True,
    **json_kwargs
)

Load object from file

Parameters:

Name Type Description Default
fp Union[TextIO, str]

the file path or file object

required
use_decimal bool

use decimal.Decimal for numbers. Defaults to True.

True
**json_kwargs

other arguments passed to json.load

{}

Returns:

Name Type Description
Any

the deserialized object

Source code in .venv/lib/python3.12/site-packages/bloqade/analog/serialize.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
@beartype
def load(fp: Union[TextIO, str], use_decimal: bool = True, **json_kwargs):
    """Load object from file

    Args:
        fp (Union[TextIO, str]): the file path or file object
        use_decimal (bool, optional): use decimal.Decimal for numbers. Defaults to True.
        **json_kwargs: other arguments passed to json.load

    Returns:
        Any: the deserialized object
    """
    load_bloqade()
    if isinstance(fp, str):
        with open(fp, "r") as f:
            return json.load(
                f,
                object_hook=Serializer.object_hook,
                use_decimal=use_decimal,
                **json_kwargs,
            )
    else:
        return json.load(
            fp,
            object_hook=Serializer.object_hook,
            use_decimal=use_decimal,
            **json_kwargs,
        )

loads

loads(s: str, use_decimal: bool = True, **json_kwargs)

Load object from string

Parameters:

Name Type Description Default
s str

the string to load

required
use_decimal bool

use decimal.Decimal for numbers. Defaults to True.

True
**json_kwargs

other arguments passed to json.loads

{}

Returns:

Name Type Description
Any

the deserialized object

Source code in .venv/lib/python3.12/site-packages/bloqade/analog/serialize.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@beartype
def loads(s: str, use_decimal: bool = True, **json_kwargs):
    """Load object from string

    Args:
        s (str): the string to load
        use_decimal (bool, optional): use decimal.Decimal for numbers. Defaults to True.
        **json_kwargs: other arguments passed to json.loads

    Returns:
        Any: the deserialized object
    """
    load_bloqade()
    return json.loads(
        s, object_hook=Serializer.object_hook, use_decimal=use_decimal, **json_kwargs
    )

save

save(
    o: Any,
    fp: Union[TextIO, str],
    use_decimal=True,
    **json_kwargs
) -> None

Serialize object to file

Parameters:

Name Type Description Default
o Any

the object to serialize

required
fp Union[TextIO, str]

the file path or file object

required
use_decimal bool

use decimal.Decimal for numbers. Defaults to True.

True
**json_kwargs

other arguments passed to json.dump

{}

Returns:

Type Description
None

None

Source code in .venv/lib/python3.12/site-packages/bloqade/analog/serialize.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@beartype
def save(
    o: Any,
    fp: Union[TextIO, str],
    use_decimal=True,
    **json_kwargs,
) -> None:
    """Serialize object to file

    Args:
        o (Any): the object to serialize
        fp (Union[TextIO, str]): the file path or file object
        use_decimal (bool, optional): use decimal.Decimal for numbers. Defaults to True.
        **json_kwargs: other arguments passed to json.dump

    Returns:
        None
    """
    if not isinstance(o, Serializer.types):
        raise TypeError(
            f"Object of type {type(o)} is not JSON serializable. "
            f"Only {Serializer.types} are supported."
        )
    if isinstance(fp, str):
        with open(fp, "w") as f:
            json.dump(o, f, cls=Serializer, use_decimal=use_decimal, **json_kwargs)
    else:
        json.dump(o, fp, cls=Serializer, use_decimal=use_decimal, **json_kwargs)