Skip to content

Serializer

serialize_to_json(obj)

Serializes a dataclass object to a JSON string.

Parameters:

Name Type Description Default
obj dataclass

A dataclass object to be serialized.

required

Returns:

Name Type Description
str str

A JSON string representing the serialized data from the dataclass object.

Source code in libs/python/shared/py-serializer/pyserializer/serializer.py
def serialize_to_json(obj: dataclass) -> str:
    """
    Serializes a dataclass object to a JSON string.

    Args:
        obj: A dataclass object to be serialized.

    Returns:
        str: A JSON string representing the serialized data from the dataclass object.
    """
    data = _get_serialized_object(obj)
    return json.dumps(data, sort_keys=True)

serialize_to_dict(obj)

Serializes a dataclass object to a dictionary.

Parameters:

Name Type Description Default
obj dataclass

A dataclass object to be serialized.

required

Returns:

Name Type Description
dict Dict[str, any]

A dictionary containing the serialized data from the dataclass object.

Source code in libs/python/shared/py-serializer/pyserializer/serializer.py
def serialize_to_dict(obj: dataclass) -> Dict[str, any]:
    """
    Serializes a dataclass object to a dictionary.

    Args:
        obj: A dataclass object to be serialized.

    Returns:
        dict: A dictionary containing the serialized data from the dataclass object.
    """
    return _get_serialized_object(obj)

serialize_to_dataclass(data, cls)

Deserializes data from a dictionary into a dataclass object.

Parameters:

Name Type Description Default
data Dict[str, any]

A dictionary containing data to be deserialized.

required
cls Type

The dataclass type to which the data should be deserialized.

required

Returns:

Name Type Description
cls dataclass

An instance of the specified dataclass type with data deserialized from the input dictionary.

Source code in libs/python/shared/py-serializer/pyserializer/serializer.py
def serialize_to_dataclass(data: Dict[str, any], cls: Type) -> dataclass:
    """
    Deserializes data from a dictionary into a dataclass object.

    Args:
        data: A dictionary containing data to be deserialized.
        cls: The dataclass type to which the data should be deserialized.

    Returns:
        cls: An instance of the specified dataclass type with data deserialized from the input dictionary.
    """
    args = {}
    for field_obj in fields(cls):
        field_name = field_obj.name
        field_metadata = field_obj.metadata
        json_name = field_metadata.get("json_name", field_name)

        if json_name in data:
            args[field_name] = data[json_name]

    return cls(**args)