Skip to content

declearn.utils.serialize_object

Return a ObjectConfig serialization of an object.

This function is the counterpart to declearn.utils.deserialize_object.

Parameters:

Name Type Description Default
obj SupportsConfig

Object that needs serialization. To be valid, the object must: - implement the get_config and from_config (class)methods - belong to a registered type, unless allow_unregistered=True (i.e. a type that has been passed to or decorated with the declearn.utils.register_type function)

required
group Optional[str]

Optional name of the group under which the object's type was registered. If None, the type will be looked for in each and every exiting group, and the first match will be used.

None
allow_unregistered bool

Whether to allow serializing objects that do not belong to a registered type. If true, a mapping between the type's name and its class will need to be passed as part of custom to the deserialization function (see deserialize_object).

False

Returns:

Name Type Description
config ObjectConfig

A JSON-serializable dataclass wrapper containing all required information to recreate the object, e.g. from a config file.

Raises:

Type Description
KeyError

If obj's type is not registered and allow_unregistered=False.

Source code in declearn/utils/_serialize.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def serialize_object(
    obj: SupportsConfig,
    group: Optional[str] = None,
    allow_unregistered: bool = False,
) -> ObjectConfig:
    """Return a ObjectConfig serialization of an object.

    This function is the counterpart to `declearn.utils.deserialize_object`.

    Parameters
    ----------
    obj: object
        Object that needs serialization. To be valid, the object must:
        - implement the `get_config` and `from_config` (class)methods
        - belong to a registered type, unless `allow_unregistered=True`
          (i.e. a type that has been passed to or decorated with the
          `declearn.utils.register_type` function)
    group: str or None, default=None
        Optional name of the group under which the object's type was
        registered. If None, the type will be looked for in each and
        every exiting group, and the first match will be used.
    allow_unregistered: bool, default=False
        Whether to allow serializing objects that do not belong to a
        registered type. If true, a mapping between the type's name
        and its class will need to be passed as part of `custom` to
        the deserialization function (see `deserialize_object`).

    Returns
    -------
    config: ObjectConfig
        A JSON-serializable dataclass wrapper containing all required
        information to recreate the object, e.g. from a config file.

    Raises
    ------
    KeyError
        If `obj`'s type is not registered and `allow_unregistered=False`.
    """
    try:
        name, group = access_registration_info(type(obj), group)
    except KeyError as exception:
        if allow_unregistered:
            name = type(obj).__name__
            group = None
        else:
            raise exception
    config = obj.get_config()
    return ObjectConfig(name, group, config)