Hacker News new | ask | show | jobs
by kopos 3833 days ago
If a Python class overrides __getattr__() or has a complex deeper hierarchy of inheritance, pickle sometimes fails in spectacular ways. Sometimes the fixes were extremely painful and sort of nullified the purpose of having a __getattr__() in the first place. More importantly, the serialized data that pickle generates (1) is in python bytecode and is not easy to humanly reason with during debugging (2) a simple object will get inflated because of a deeper inheritance tree.

However by using marshmallow for serializing, I found the resultant JSON output a much more manageable output format to 'reason with'. In my specific case where the lifetime of a python object could be extended over multiple sessions and pass through the runtime -> save -> db -> load -> runtime barrier, JSON was a hugely meaningful choice. The project used a graph of connected Python objects whose relations & states needed to be retained over time and memory barriers.

With a general HTTP REST API, I find that bundling all the related records of an entity in a single API call saves up the roundabout time for the client. In this case, instead of hand building a dict and then generating a json.dumps() I find using marshmallow a better choice - the declaration of the serializer itself, reflects the structure & the entity relations clearly.