Part 3 · Section 07
Serialization and Deserialization
A dictionary in memory is a web of objects connected by references. The moment you want to save it to disk, send it over a network or hand it to a program written in another language, that web has to be flattened into a sequence of bytes or characters: it has to be serialized. The reverse step, rebuilding objects from that sequence, is deserialization (together they’re often called marshalling). It sounds mechanical, but the details decide what survives the round trip, what gets silently changed, and whether loading data can hurt you.
In this section you’ll learn:
- how
pickleserializes almost any Python object, what it preserves, and why you must never unpickle untrusted data - how the
jsonmodule maps Python types to JSON, and the conversions that makeloads(dumps(d)) != d - how to encode types JSON doesn’t know (dates, decimals, sets, your own classes) with a
defaultfunction,functools.singledispatchor aJSONEncodersubclass - how to decode them again with
object_hook,object_pairs_hook, theparse_*hooks andJSONDecoder - how to describe and enforce the shape of JSON documents with JSON Schema and the
jsonschemalibrary - what schema-based libraries such as marshmallow and serpy add, and how to get the same effect with the standard library
- how YAML compares with JSON, and how to load it safely with PyYAML