How Python’s singledispatch Simplifies Custom JSON Serialization

This article explores Python’s underused singledispatch feature from PEP 443 as a clean, extensible way to serialize arbitrary objects to JSON, comparing it with traditional json module approaches and showing how it avoids cumbersome if‑elif‑else logic while keeping classes untouched.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How Python’s singledispatch Simplifies Custom JSON Serialization

Many Python standard‑library modules contain hidden gems, and one of the most useful is the type‑based function dispatch provided by functools.singledispatch. It is ideal for serializing arbitrary objects, such as when producing JSON for web APIs or structured logs.

Traditional JSON Serialization

The built‑in json module (inherited from simplejson) offers two ways to serialize objects: (1) implement a default() function that receives an object and returns something the JSONEncoder can understand; (2) subclass JSONEncoder and pass it via the cls argument to dump. Both approaches require you to know every custom type in advance, making the code brittle and hard to extend.

Because third‑party libraries often mimic the json API for compatibility, they inherit the same limitations.

Extensibility Issues

All of the above methods share a lack of extensibility: they do not provide support for new types without modifying a central default() function. Writing a monolithic function that handles every custom type quickly becomes painful.

Some projects, like Pyramid’s JSON renderer, use the adapter registry from zope.interface to achieve a more modular solution. Django supplies DjangoJSONEncoder, a subclass of json.JSONEncoder, which knows how to encode dates, times, UUIDs, etc., but beyond that you still need to implement your own logic.

Another approach, used by structlog, adds a __structlog__ method to classes, similar to __str__, but this requires modifying the classes themselves and is generally discouraged.

Enter PEP 443

Python 3.4 introduced a better solution via PEP 443: functools.singledispatch, with back‑ports available on PyPI for older versions.

With singledispatch, you define a generic function and then register type‑specific implementations. For example, you can register a handler for datetime objects that calls a to_serializable() method, and the dispatcher will automatically select the correct function.

This approach lets you place your serializer wherever you like—inside a class, in a dedicated module, or alongside other JSON‑related code—while keeping your classes clean and avoiding large if‑elif‑else blocks.

A Deeper Look

The @singledispatch decorator is more powerful than just JSON serialization; it provides a general mechanism for binding different behaviors to different types, a pattern that many developers overlook.

Footnotes:

UltraJSON does not support custom object serialization; python-rapidjson only supports a default() function.

Using the attrs library can simplify management of custom types.

Pyramid’s API, after being ported from zope.component, still lacks proper documentation.

The original motivation for adding singledispatch to the standard library was a more elegant implementation of pprint, though that never materialized.

Original article link: https://hynek.me/articles/serialization/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPythonserializationfunctoolssingledispatch
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.