Skip to content

Serialize

dumps

dumps(o, use_decimal=True, **json_kwargs)

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 src/bloqade/serialize.py
@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, use_decimal=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 src/bloqade/serialize.py
@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, use_decimal=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 src/bloqade/serialize.py
@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, fp, use_decimal=True, **json_kwargs)

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 src/bloqade/serialize.py
@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)