Package: deserialize
deserializeA library to make deserialization easy. To get started, just runpip install deserializeHow it used to beWithout the library, if you want to convert:{"a":1,"b":2}into a dedicated class, you had to do something like this:classMyThing:def__init__(self,a,b):self.a=aself.b=b@staticmethoddeffrom_json(json_data):a_value=json_data.get("a")b_value=json_data.get("b")ifa_valueisNone:raiseException("'a' was None")elifb_valueisNone:raiseException("'b' was None")eliftype(a_value)!=int:raiseException("'a' was not an int")eliftype(b_value)!=int:raiseException("'b' was not an int")returnMyThing(a_value,b_value)my_instance=MyThing.from_json(json_data)How it is nowWithdeserializeall you need to do is this:importdeserializeclassMyThing:a:intb:intmy_instance=deserialize.deserialize(MyThing,json_data)That's it. It will pull out all the data and set it for you type checking and even checking for null values.If you want null values to be allowed though, that's easy too:fromtypingimportOptionalclassMyThing:a:Optional[int]b:Optional[int]NowNoneis a valid value for these.Types can be nested as deep as you like. For example, this is perfectly valid:classActor:name:strage:intclassEpisode:title:stridentifier:stactors:List[Actor]classSeason:episodes:List[Episode]completed:boolclassTVShow:seasons:List[Season]creator:strAdvanced UsageCustom KeysIt may be that you want to name your properties in your object something different to what is in the data. This can be for readability reasons, or because you have to (such as if your data item is named__class__). This can be handled too. Simply use thekeyannotation as follows:@deserialize.key("identifier","id")classMyClass:value:intidentifier:strThis will now assign the data with the keyidto the fieldidentifier. You can have multiple annotations to override multiple keys.Auto SnakeData will often come in with the keys either camelCased or PascalCased. Since Python uses snake_case as standard for members, this means that custom keys are often used to do the conversion. To make this easier, you can add theauto_snakedecorator and it will do this conversion for you where it can.@deserialize.auto_snake()classMyClass:some_integer:intsome_string:strNow you can pass this data and it will automatically parse:{"SomeInteger":3,"SomeString":"Hello"}Note that all fields need to be snake cased if you use this decorator.Unhandled FieldsUsually, if you don't specify the field in your definition, but it does exist in the data, you just want to ignore it. Sometimes however, you want to know if there is extra data. In this case, when callingdeserialize(...)you can setthrow_on_unhandled=Trueand it will raise an exception if any fields in the data are unhandled.Additionally, sometimes you want this, but know of a particular field that can be ignored. You can mark these as allowed to be unhandled with the decorator@allow_unhandled("key_name").Ignored KeysYou may want some properties in your object that aren't loaded from disk, but instead created some other way. To do this, use theignoredecorator. Here's an example:@deserialize.ignore("identifier")classMyClass:value:intidentifier:strWhen deserializing, the library will now ignore theidentifierproperty.ParsersSometimes you'll want something in your object in a format that the data isn't in. For example, if you get the data:{"successful":True,"timestamp":1543770752}You may want that to be represented as:classResult:successful:booltimestamp:datetime.datetimeBy default, it will fail on this deserialization as the value in the data is not a timestamp. To correct this, use theparserdecorator to tell it a function to use to parse the data. E.g.@deserialize.parser("timestamp",datetime.datetime.fromtimestamp)classResult:successful:booltimestamp:datetime.datetimeThis will now detect when handling the data for thekeytimestampand run it through the parser function supplied before assigning it to your new class instance.The parser is runbeforetype checking is done. This means that if you had something likeOptional[datetime.datetime], you should ensure your parser can handle the value beingNone. Your parser will obviously need to return the type that you have declared on the property in order to work.SubclassingSubclassing is supported. If you have a typeShapefor example, which has a subclassRectangle, any properties onShapeare supported if you try and decode some data into a `rectangle object.Raw StorageIt can sometimes be useful to keep a reference to the raw data that was used to construct an object. To do this, simply set theraw_storage_modeparamater toRawStorageMode.ROOTorRawStorageMode.ALL. This will store the data in a parameter named__deserialize_raw__on the root object, or on all objects in the tree respectively.DefaultsSome data will come to you with fields missing. In these cases, a default is often known. To do this, simply decorate your class like this:@deserialize.default("value",0)classIntResult:successful:boolvalue:intIf you pass in data like{"successful": True}this will deserialize to a default value of0forvalue. Note, that this would not deserialize sincevalueis not optional:{"successful": True, "value": None}.Post-processingNot everything can be set on your data straight away. Some things need to be figured out afterwards. For this you need to do some post-processing. The easiest way to do this is through the@constructeddecorator. This decorator takes a function which will be called whenever a new instance is constructed with that instance as an argument. Here's an example which converts polar coordinates from using degrees to radians:data={"angle":180.0,"magnitude":42.0}defconvert_to_radians(instance):instance.angle=instance.angle*math.pi/180@deserialize.constructed(convert_to_radians)classPolarCoordinate:angle:floatmagnitude:floatpc=deserialize.deserialize(PolarCoordinate,data)print(pc.angle,pc.magnitude)>>>3.14159265358979342.0DowncastingData often comes in the form of having the type as a field in the data. This can be difficult to parse. For example:data=[{"data_type":"foo","foo_prop":"Hello World",},{"data_type":"bar","bar_prop":"Goodbye World",}]Since the fields differ between the two, there's no good way of parsing this data. You could use optional fields on some base class, try multiple deserializations until you find the right one, or do the deserialization based on a mapping you build of thedata_typefield. None of those solutions are elegant though, and all have issues if the types are nested. Instead, you can use thedowncast_fieldanddowncast_identifierdecorators.downcast_fieldis specified on a base class and gives the name of the field that contains the type information.downcast_identifiertakes in a base class and an identifier (which should be one of the possible values of thedowncast_fieldfrom the base class). Internally, when a class with a downcast field is detected, the field will be extacted, and a subclass with a matching identifier will be searched for. If no such class exists, anUndefinedDowncastExceptionwill be thrown.Here's an example which would handle the above data:@deserialize.downcast_field("data_type")classMyBase:type_name:str@deserialize.downcast_identifier(MyBase,"foo")classFoo(MyBase):foo_prop:str@deserialize.downcast_identifier(MyBase,"bar")classBar(MyBase):bar_prop:strresult=deserialize.deserialize(List[MyBase],data)Here,result[0]will be an instance ofFooandresult[1]will be an instance ofBar.If you can't describe all of your types, you can use@deserialize.allow_downcast_fallbackon your base class and any unknowns will be left as dictionaries.