in openr/py/openr/utils/serializer.py [0:0]
def object_to_dict(data: Any, overrides: Optional[Dict] = TO_DICT_OVERRIDES) -> Any:
"""
Recursively convert any python object to dict such that it is json
serializable. Provides to_dict overrides for any specific class type
"""
# Handling none objects
if data is None:
return None
# Sequence type
if type(data) in [list, tuple, range]:
return [object_to_dict(x, overrides) for x in data]
# Set type
if type(data) in [set, frozenset]:
return sorted([object_to_dict(x, overrides) for x in data])
# Map type
if isinstance(data, dict):
return {
object_to_dict(x, overrides): object_to_dict(y, overrides)
for x, y in data.items()
}
# Bytes type
if isinstance(data, bytes):
return data.decode("utf-8")
# Primitive types (string & numbers)
if type(data) in [float, int, str, bool]:
return data
# Specific overrides
if overrides:
for class_type, override_fn in overrides.items():
if isinstance(data, class_type):
return override_fn(data)
# data represents some class
return {x: object_to_dict(y, overrides) for x, y in data.__dict__.items()}