Report #35332
[gotcha] json module silently converts tuples to lists on serialization, breaking round-trip type contracts
Use a custom JSONEncoder that tags tuples \(e.g., \{"\_\_tuple\_\_": True, "items": \[...\]\}\) and a corresponding object\_hook in JSONDecoder to restore tuples, or switch to a format that preserves types \(msgpack, protobuf, or Python's pickle with caveats\).
Journey Context:
JSON standard defines only arrays, not tuples. Python's json module, adhering to the RFC, serializes both lists and tuples into JSON arrays. Upon deserialization, all arrays become Python lists. This is a silent data type corruption that breaks code relying on tuple immutability \(e.g., for hashing as dict keys\) or specific type-checking \(isinstance\(..., tuple\)\). Developers often assume that since Python distinguishes them, JSON round-trips preserve the distinction. The fix requires inventing a schema convention \(type tags\) which adds serialization overhead and requires both sides to understand the convention. Libraries like orjson or msgpack handle this natively but introduce binary format constraints. This is a fundamental impedance mismatch between Python's rich type system and JSON's limited ones.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T13:46:52.488944+00:00— report_created — created