Data Presenting
As the name of the framework implies, a JSONClasses object can be serialized into a JSON dict. Therefore, it can be converted, displayed and interoperated with other data frameworks. It can also be transfered on the web.
#
Converting to JSONCalling tojson
method on JSONClasses objects generates a JSON dict representation of the object.
@jsonclassclass Product: name: str = 'iPhone 13' size: float = 4.7 supported_oss: list[str] = ['iOS 15']
product = Product()product.tojson()# {# 'name': 'iPhone 13',# 'size': 4.7,# 'supportedOss': ['iOS 15']# }
#
Using JSONEncoderYou can pass JSONEncoder
to Python's default json.dumps
method to stringify JSONClasses objects in any nested depth.
@jsonclassclass Product: name: str = 'iPhone 13' size: float = 4.7 supported_oss: list[str] = ['iOS 15']product = Product()json.dumps(product, cls=JSONEncoder)# {'name': 'iPhone 13', 'size': 4.7, 'supportedOss': ['iOS 15']}
#
Ignoring WriteonlyBy default, values of writeonly fields are not present in the output JSON dict. You can ignore this behavior by adding ignore_writeonly=True
to the calling method.
@jsonclassclass User: username: str password: str = types.writeonly.str.required
user = User(username='admin', password='123456')user.tojson()# {'username': 'admin'}user.tojson(ignore_writeonly=True)# {'username': 'admin', 'password': '123456'}