ASerializer¶
-
class
ajson.aserializer.
ASerializer
(max_depth=15)[source]¶ Serialize and unserialize objects
-
add_serialize_handler
(_type: Type, handler: NewType.<locals>.new_type)[source]¶ Adds a handler for a specific type to modify the way it should be serialize
>>> serializer = ASerializer() >>> serializer.add_serialize_handler(int, lambda obj, *args: obj if obj > 0 else 0) # negative ints return 0 >>> serializer.serialize(5) '5' >>> serializer.serialize(-6) '0'
-
from_dict
(dict_obj: Any, _type: Optional[Type] = None, *init_args_array, **init_kargs) → Any[source]¶ Creates an object with the type _type from a dictionary groups will be ignored for unserialization
Parameters: - dict_obj – dict to be transformed into an object
- _type – Resulting type of the object to construct
- init_args_array – construct args list to initialize the object with type _type
- init_kargs – construct args to initialize the object with type _type
>>> from ajson import AJson >>> serializer = ASerializer() >>> @AJson() ... class Car: ... max_speed: float # @aj(') ... brand: str # @aj()
>>> car: Car = serializer.from_dict({'max_speed': 100, 'brand': 'Jeep'}, Car) >>> car.max_speed 100 >>> car.brand 'Jeep'
-
serialize
(obj, groups: Optional[List[str]] = None) → str[source]¶ Creates a json string from the obj
Parameters: - obj – Object to be serialize
- groups – list of groups that determines what attributes should be serialize
>>> from ajson import AJson >>> serializer = ASerializer() >>> @AJson() ... class House: ... rooms_num: int # @aj(groups='["public", "owner"]') ... square_meters: int # @aj(groups='["owner"]') ... def __init__(self, rooms_num, square_meters): ... self.rooms_num = rooms_num ... self.square_meters = square_meters
>>> serializer.serialize(House(3, 100), groups=['public']) '{"room_num": 3}' >>> serializer.serialize(House(3, 100), groups=['owner']) '{"room_num": 3, "square_meters": 100}'
-
to_dict
(obj, groups: Optional[List[str]] = None) → Union[Dict[str, Any], List][source]¶ Same as serialize, but it creates a serializable dict instead of a str
Parameters: - obj – Object to be serialize
- groups – list of groups that determines what attributes should be serialize
>>> from ajson import AJson >>> serializer = ASerializer() >>> @AJson() ... class Car: ... max_speed: float # @aj(groups='["basic", "detailed"]') ... brand: str # @aj(groups='["detailed"]') ... def __init__(self, max_speed, brand): ... self.max_speed = max_speed ... self.brand = brand
>>> serializer.to_dict(Car(140, 'ford'), groups=['basic']) {"max_speed": 140} >>> serializer.to_dict(Car(140, 'ford'), groups=['detailed']) {"max_speed": 140, "brand": 7}
-
unserialize
(json_str: str, _type: Optional[Type] = None, *init_args_array, **init_kargs) → Any[source]¶ Creates an object with the type _type from a string groups will be ignored for unserialization
Parameters: - json_str – string to be transformed into an object
- _type – Resulting type of the object to construct
- init_args_array – construct args list to initialize the object with type _type
- init_kargs – construct args to initialize the object with type _type
>>> from ajson import AJson >>> serializer = ASerializer() >>> @AJson() ... class House: ... rooms_num: int # @aj() ... square_meters: int # @aj()
>>> house: House = serializer.unserialize('{"rooms_num": 1, "square_meters":50}', House) >>> house.rooms_num 1 >>> house.square_meters 50
-