Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can dataclasses be serialized just as easily?


In addition to the already-mentioned suggestions, you can also use marshmallow schemas to serialise complex objects, and there's even a package to autogen marshmallow schemas from dataclasses: https://pypi.org/project/marshmallow-dataclass/


Yep! Both msgspec (https://jcristharif.com/msgspec/supported-types.html#datacla...) and orjson support encoding dataclasses to JSON natively.


Yes via pickle.

Or for JSON, dataclasses_json


Be aware that pickle is unsafe (as documented near the top of https://docs.python.org/3/library/pickle.html), so prefer other serialization formats except between trusted processes


I can't remember the last time I _wanted_ to pickle something. Parquet and JSON pretty much cover it for me. I guess pickling made more sense before tools like Pydantic were good and popular?

If I want all the python-specific nuance on disk, I know I'm in a bad place!


I use pickle to serialize RPC requests between internal services. Nothing beats it in terms of speed and ease of use.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: