@File : serializer.py @Time : 2025/04/02 16:00:58 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2025, Alejandro Marrero @Desc : None

CustomJSONEncoder

Bases: JSONEncoder

Custom JSON encoder to handle complex types like NumPy arrays and custom objects.

Source code in digneapy/utils/serializer.py
63
64
65
66
67
68
69
70
71
72
class CustomJSONEncoder(json.JSONEncoder):
    """
    Custom JSON encoder to handle complex types like NumPy arrays and custom objects.
    """

    def default(self, obj):
        try:
            return serialize(obj)
        except TypeError:
            return super().default(obj)

serialize(obj)

Recursively serialize an object to a dictionary. Handles nested objects, lists, and dictionaries.

Source code in digneapy/utils/serializer.py
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
def serialize(obj):
    """
    Recursively serialize an object to a dictionary.
    Handles nested objects, lists, and dictionaries.
    """
    if isinstance(obj, (int, float, str, bool, type(None))):
        return obj  # Primitive types are directly serializable

    if (
        isinstance(obj, (FunctionType))
        or type(obj).__name__ == "cython_function_or_method"
    ):
        return obj.__name__

    if isinstance(obj, (list, tuple, dict)):
        return [serialize(item) for item in obj]

    # if isinstance(obj, (list, tuple)) or isinstance(obj, dict):
    #     return [
    #         serialize(item) for item in obj
    #     ]  # Serialize each element in the list/tuple

    if isinstance(obj, dict):
        return {
            key: serialize(value) for key, value in obj.items()
        }  # Serialize each key-value pair

    if isinstance(obj, np.ndarray):
        return obj.tolist()  # Convert NumPy arrays to lists

    if hasattr(obj, "__dict__"):  # Handle custom objects
        return {
            key: serialize(value)
            for key, value in vars(obj).items()
            if not key.startswith("_")
        }
    if hasattr(obj, "__slots__"):
        return {slot: serialize(getattr(obj, slot)) for slot in obj.__slots__}

    return str(obj)  # Fallback: Convert to string for unsupported types

to_json(obj)

Convert an object to a JSON string.

Source code in digneapy/utils/serializer.py
75
76
77
78
79
def to_json(obj):
    """
    Convert an object to a JSON string.
    """
    return json.dumps(serialize(obj), cls=CustomJSONEncoder, indent=4)