Hacker News new | ask | show | jobs
by kopos 3833 days ago
Used it on a huge project where I wanted the interfaces clean between the Python project at runtime and to serialize the Python objects to the database. Using pickling gave me sleepless night until I moved to Marshmallow. Whilst Django REST Framework comes with a nice serializer and parser modules, marshmallow's ability to do just the part was a godsend.

This alone gave us the flexibility to expose the Python modules and objects as a simple JSON API and the DB load / save came for free.

Highly recommended.

1 comments

can you talk about the usecase a little more. I'm trying to figure out why you would use marshmallow instead of pickling.. especially in context of building REST api.

I use Flask and I am not sure where pickling comes in. I have built a desktop application in pyqt though and the multiprocessing modules need pickle-able data.

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.