Package: serdelicacy
serdelicacySerialize (serdelicacy.dump) and deserialize (serdelicacy.load) from/to strongly-typed, native Python data structures.Read the latest documentationhereFeaturesEffortless deserialization of unstructured Python types into structured, type-hinted Python types (dataclasses.dataclass,typing.NamedTuple)Effortless serialization of structured, type-hinted Python objects into unstructured Python types (eg, the reverse)Clear error messages when serde fails at runtimeNo inherited, non-standard types. dataclasses, NamedTuples, and other standard Python types are bread and butterEditor support: I like my autocompletion, so I jump through lots of hoops to make this library compatible with JediHandleoptional propertieswith a domain-specificserdelicacy.OptionalPropertyEnable customization through sophisticated validation, deserialization overrides, and serialization overrides for dataclasses.Require no 3rd party dependencies; Python 3.8+Installation# With pippipinstallserdelicacy# With poetrypoetryaddserdelicacyUsageSeeexamples folder.Validation / transformation for dataclassesCustomization override options are available for validations and transformations on both deserialization and serialization. Custom overrides are available fordataclassesthrough themetadataargument to thedataclasses.fieldfunction:fromdataclassesimportdataclass,fieldimportserdelicacyfromserdelicacyimportOverridedef_is_long_enough(value)->None:iflen(value)<4:raiseValueError(f"'{value}' is not enough characters")VALUE={"firstname":"richard","lastname":"spencerson"}@dataclassclassPerson:firstname:str=field(metadata={"serdelicacy":Override(validate=_is_long_enough,transform_load=str.title,)})lastname:str=field(metadata={"serdelicacy":Override(validate=_is_long_enough,transform_load=str.title,transform_dump=str.upper,)})print(serdelicacy.load(VALUE,Person))As suggested by the Pythondataclasses.field documentation, allserdelicacy-related field metadata is namespaced to 1 dictionary key:serdelicacy. Its value should be of typeserdelicacy.Override, a dataclass itself whose fields are the following:validate:Callable[[Any], NoReturn], Callable[[Any], bool]: a function that either a) returns a boolean where False indicates failed validation or b) nothing, but raises Python exceptions on validation failure. Is executed as the final step of a value's load, after all transformations have been completed. By default, this is a function that does nothing.transform_load:Callable[[Any], Any]. This transformation is executed before any other loading takes place. By default, this is anidentity functiontransform_postload: this should beCallable[[T], T]], whereTis the type of the field. This transformation is executed after all recursive loading takes place as the final step before the value is returned for upstream processing. By default, this is anidentity functiontransform_dump: this should beCallable[[T], Any]], whereTis the type of the field. This function is executed before a value is recursively serialized. By default, this is anidentity functionFinally, you may not need to use these tools initially, but if you have strict validation or transformation requirements on your project, you'll be extremely happy they're hereFAQMy JSON keys contain whitespace, etcSimple solution: usetypeing.TypeDict'sbackwards-compatibility syntax.frompprintimportpprintfromtypingimportList,TypedDictimportserdelicacyfromserdelicacyimportOptionalPropertyDATA=[{"weird, key":1,"normal":2,},{"normal":3,},]DataItem=TypedDict("DataItem",{"weird, key":OptionalProperty[int],"normal":int,},)LOADED=serdelicacy.load(DATA,List[DataItem])print("Loaded data:")pprint(LOADED)print("Re-serialized data:")pprint(serdelicacy.dump(LOADED))This prints the following to the console.Loaded data:
[{'normal': 2, 'weird, key': 1},
 {'normal': 3, 'weird, key': <Missing property>}]
Re-serialized data:
[{'normal': 2, 'weird, key': 1}, {'normal': 3}]Try changing values in your JSON data; you'll get runtime errors if your data does not conform to the above schema. Additionally,mypyshould call out any misused variable keys / types. In short, this has enabled a type-safe load and a perfectly sane dump.Local DevelopmentLocal development for this project is simple.DependenciesInstall the following tools manually.PoetryGNU MakeRecommendedasdfSet up development environmentmakesetupRun TestsmaketestNotesInitially inspired byundictifyand a PR I helped with. serdelicacy's goals are different; it's focused on serde instead of general function signature overrides.I also notice some striking similarities with a library calledtypedload(great minds think alike, I guess :p). I renamed my top-level functions to "load" and "dump" in typedload's homage. Unfortunately, as of version1.20, typedload does not handle all types of dataclasses elegantly (mainly, InitVar). Since typedload supports Python 3.5+, it never will elegantly handle all dataclasses without lots of unfortunate conditionals in the codebase. If you must use Python 3.7-, I suggest looking into typedload.Written bySamuel Roecasamuel.roeca@gmail.com