three.serialization
1# import logging 2from array import array 3import json 4from google.protobuf.json_format import MessageToDict 5from enum import Enum 6 7# Configure logging 8# logging.basicConfig(level=logging.DEBUG) 9# logger = logging.getLogger(__name__) 10 11def Serializer(object, visited=None): 12 if visited is None: 13 visited = set() 14 15 # logger.debug(f"Serializing object: {object} (id: {id(object)})") 16 17 # Only track objects for circular references 18 if isinstance(object, (dict, list, set, tuple, Enum)) or hasattr(object, '__dict__'): 19 if id(object) in visited: 20 return None 21 visited.add(id(object)) 22 23 # Handle enums 24 if isinstance(object, Enum): 25 return object.name 26 27 # Array is not JSON serializable => Convert to list 28 if isinstance(object, array): 29 return object.tolist() 30 31 # Protobuf object 32 if hasattr(object, "DESCRIPTOR"): 33 dic = MessageToDict( 34 object, 35 preserving_proto_field_name=True, 36 including_default_value_fields=True 37 ) 38 return dict(filter(lambda tup: tup[1] is not None, dic.items())) 39 40 # Our objects 41 if hasattr(object, '__dict__'): 42 dic = dict(filter(lambda tup: tup[1] is not None, object.__dict__.items())) 43 for key, value in dic.items(): 44 dic[key] = Serializer(value, visited) 45 return dic 46 47 # Handle other types if necessary 48 return object 49 50def TO_JSON(object) -> str: 51 """ 52 Serialize an object into a json string. 53 54 Args: 55 object: the object to serialize. 56 57 Returns: 58 The string representing the object. 59 60 """ 61 return json.dumps( 62 object, 63 default=Serializer, 64 allow_nan=False 65 )
def
Serializer(object, visited=None):
12def Serializer(object, visited=None): 13 if visited is None: 14 visited = set() 15 16 # logger.debug(f"Serializing object: {object} (id: {id(object)})") 17 18 # Only track objects for circular references 19 if isinstance(object, (dict, list, set, tuple, Enum)) or hasattr(object, '__dict__'): 20 if id(object) in visited: 21 return None 22 visited.add(id(object)) 23 24 # Handle enums 25 if isinstance(object, Enum): 26 return object.name 27 28 # Array is not JSON serializable => Convert to list 29 if isinstance(object, array): 30 return object.tolist() 31 32 # Protobuf object 33 if hasattr(object, "DESCRIPTOR"): 34 dic = MessageToDict( 35 object, 36 preserving_proto_field_name=True, 37 including_default_value_fields=True 38 ) 39 return dict(filter(lambda tup: tup[1] is not None, dic.items())) 40 41 # Our objects 42 if hasattr(object, '__dict__'): 43 dic = dict(filter(lambda tup: tup[1] is not None, object.__dict__.items())) 44 for key, value in dic.items(): 45 dic[key] = Serializer(value, visited) 46 return dic 47 48 # Handle other types if necessary 49 return object
def
TO_JSON(object) -> str:
51def TO_JSON(object) -> str: 52 """ 53 Serialize an object into a json string. 54 55 Args: 56 object: the object to serialize. 57 58 Returns: 59 The string representing the object. 60 61 """ 62 return json.dumps( 63 object, 64 default=Serializer, 65 allow_nan=False 66 )
Serialize an object into a json string.
Args: object: the object to serialize.
Returns: The string representing the object.