Package: jsondaora
jsondaoraInteroperatesdataclassesandTypedDictannotations withjson objects for pythonDocumentation:https://dutradda.github.io/jsondaoraSource Code:https://github.com/dutradda/jsondaoraKey FeaturesFull compatibility withdataclassesmodule andTypedDictannotationDeserialize values from dictDeserialize values from bytes*Deserialization/serialization of chosen fieldsDeserialize the following standard types: Dict, List, Tuple, Set, Union and AnyDict serializationDirect json serialization withorjson(don't convert to dict recursively before serialization)Optional validation according with thejson-schemaspecification** feature in development.RequirementsPython 3.8+orjsonfor json serializationInstallation$ pip install jsondaoraBasic examplefromtypingimportList,TypedDictfromjsondaoraimport(as_typed_dict,asdataclass,dataclass_asjson,jsondaora,typed_dict_asjson,)@jsondaoraclassPerson:name:strage:intclassMusic:name:strmusics:List[Music]jsondict=dict(name=b'John',age='40',musics=[dict(name='Imagine')])person=asdataclass(jsondict,Person)print('dataclass:')print(person)print(dataclass_asjson(person))print()# TypedDict@jsondaoraclassMusicT(TypedDict):name:str@jsondaoraclassPersonT(TypedDict):name:strage:intmusics:List[MusicT]jsondict=dict(name=b'John',age='40',musics=[dict(name='Imagine')])person=as_typed_dict(jsondict,PersonT)print('TypedDict:')print(person)print(typed_dict_asjson(person,Person))dataclass:
Person(name='John', age=40, musics=[Person.Music(name='Imagine')])
b'{"name":"John","age":40,"musics":[{"name":"Imagine"}]}'

TypedDict:
{'name': 'John', 'age': 40, 'musics': [{'name': 'Imagine'}]}
b'{"name":"John","age":40,"musics":[{"name":"Imagine"}]}'Example for choose fields to deserializefromtypingimportList,TypedDictfromjsondaoraimport(as_typed_dict,asdataclass,dataclass_asjson,jsondaora,typed_dict_asjson,)@jsondaora(deserialize_fields=('name'))classPerson:name:strage:intclassMusic:name:strmusics:List[Music]jsondict=dict(name=b'John',age='40',musics=[dict(name='Imagine')])person=asdataclass(jsondict,Person)print('dataclass:')print(person)print(dataclass_asjson(person))print()# TypedDict@jsondaoraclassMusicT(TypedDict):name:str@jsondaora(deserialize_fields=('name',))classPersonT(TypedDict):name:strage:intmusics:List[MusicT]jsondict=dict(name=b'John',age='40',musics=[dict(name='Imagine')])person=as_typed_dict(jsondict,PersonT)print('TypedDict:')print(person)print(typed_dict_asjson(person,PersonT))dataclass:
Person(name='John', age='40', musics=[{'name': 'Imagine'}])
b'{"name":"John","age":"40","musics":[{"name":"Imagine"}]}'

TypedDict:
{'name': 'John', 'musics': [{'name': 'Imagine'}], 'age': '40'}
b'{"name":"John","musics":[{"name":"Imagine"}],"age":"40"}'Example for choose fields to serializefromtypingimportList,TypedDictfromjsondaoraimport(as_typed_dict,asdataclass,dataclass_asjson,jsondaora,typed_dict_asjson,)@jsondaora(serialize_fields=('name','age'))classPerson:name:strage:intclassMusic:name:strmusics:List[Music]jsondict=dict(name='John',age=40,musics=[dict(name='Imagine')])person=asdataclass(jsondict,Person)print('dataclass:')print(person)print(dataclass_asjson(person))print()# TypedDict@jsondaoraclassMusic(TypedDict):name:str@jsondaora(serialize_fields=('age',))classPersonT(TypedDict):name:strage:intmusics:List[Music]jsondict=dict(name=b'John',age='40',musics=[dict(name='Imagine')])person=as_typed_dict(jsondict,PersonT)print('TypedDict:')print(person)print(typed_dict_asjson(person,PersonT))dataclass:
Person(name='John', age=40, musics=[Person.Music(name='Imagine')])
b'{"age":40,"name":"John"}'

TypedDict:
{'name': 'John', 'age': 40, 'musics': [{'name': 'Imagine'}]}
b'{"age":40}'