During my first encounter of handling JSON (de)serialization in Python, I faced the problem of (de)serializing objects that have properties that are instances of another class. Using the json module, one has to write two methods, a complex_handler and a class_mapper that are fed to json.dumps and json.loads respectively. The design problem here is that the class_mapper needs to compare a dict that is to be deserialized with the properties of potential classes in order to find the matching type. In the first level of deserialization one could potentially provide the type as a parameter to the deserialization-function, but as soon as there is a child property, the type may be unknown. Therefore each class that is to be deserialized has to be registered into a collection of (properties) -> class mappings. To simplify the handling, I wrote the following JsonConvert class: Now we can define some classes and make…
Category: Python
printprev() – print Python sourcecode to console
Print your Python source code to the console. For structure, monitoring or whatever purpose you can think of.