You can use Jsonic library.
Jsonic is a lightweight utility for serializing/deserializing python objects to/from JSON.
Jsonic allows serializing/deserializing to/from specific Class instances.
It supports many of the functionalities Jackson supports in Java.
Example:
from jsonic import serialize, deserialize
class User(Serializable):
def __init__(self, user_id: str, birth_time: datetime):
super().__init__()
self.user_id = user_id
self.birth_time = birth_time
user = User('id1', datetime(2020,10,11))
obj = serialize(user) # {'user_id': 'id1', 'birth_time': {'datetime': '2020-10-11 00:00:00', '_serialized_type': 'datetime'}, '_serialized_type': 'User'}
# Here the magic happens
new_user : User = deserialize(obj) # new_user is a new instance of user with same attributes
Jsonic has some nifty features:
- You can serialize objects of types that are not extending Serializable. 2. This can come handy when you need to serialize objects of third party library classes.
- Support for custom serializers and deserializers
- Serializing into JSON string or python dict
- Transient class attributes
- Supports both serialization of private fields or leave them out of the serialization process.
Here you can find some more advanced example:
from jsonic import Serializable, register_jsonic_type, serialize, deserialize
class UserCredentials:
"""
Represents class from some other module, which does not extend Serializable
We can register it using register_serializable_type function
"""
def __init__(self, token: str, exp: datetime):
self.token = token
self.expiration_time = exp
self.calculatedAttr = random.uniform(0, 1)
# Register UserCredentials which represents class from another module that does not implement Serializable
# exp __init__ parameter is mapped to expiration_time instace attribute
register_jsonic_type(UserCredentials, init_parameters_mapping={'exp': 'expiration_time'})
class User(Serializable):
transient_attributes = ['user_calculated_attr'] # user_calculated_attr won't be serialized and deserialzied
init_parameters_mapping = {'id': 'user_id'} # id __init__ parameter is mapped to user_id instace attribute
def __init__(self, user_id: str, birth_time: datetime, user_credentials: UserCredentials, *args):
super().__init__()
self.user_id = user_id
self.birth_time = birth_time
self.user_credentials = user_credentials
self.user_calculated_attr = 'user_calculated_attr'
user = User(user_id='user_1', birth_time=datetime(1995, 7, 5, 0),
user_credentials=UserCredentials(token='token', exp=datetime(2020, 11, 1, 0)))
# Here the magic happens
user_json_obj = serialize(user, string_output=True)
new_user = deserialize(user_json_obj, string_input=True, expected_type=User)
Full disclosure: I'm the creator of Jsonic, i recommend you read the Jsonic GitHub repository README to see if it fits your needs.