Skip to main content

Custom JSON Encoder for Python utilising functools.singledispatch to support custom encoders for both Python’s built-in classes and user-created classes, without as much legwork.

Docs

Documentation Build Status Docs Check Status

Tests

Linux Test Status Windows Test Status macOS Test Status Coverage

PyPI

PyPI - Package Version PyPI - Supported Python Versions PyPI - Supported Implementations PyPI - Wheel

Anaconda

Conda - Package Version Conda - Platform

Activity

GitHub last commit GitHub commits since tagged version Maintenance PyPI - Downloads

QA

CodeFactor Grade Flake8 Status mypy status

Other

License GitHub top language Requirements Status


Usage

Creating and registering a custom encoder is as easy as:

>>> import sdjson
>>>
>>> @sdjson.dump.register(MyClass)
>>> def encode_myclass(obj):
...     return dict(obj)
>>>

In this case, MyClass can be made JSON-serializable simply by calling dict() on it. If your class requires more complicated logic to make it JSON-serializable, do that here.

Then, to dump the object to a string:

>>> class_instance = MyClass()
>>> print(sdjson.dumps(class_instance))
'{"menu": ["egg and bacon", "egg sausage and bacon", "egg and spam", "egg bacon and spam"],
"today\'s special": "Lobster Thermidor au Crevette with a Mornay sauce served in a Provencale
manner with shallots and aubergines garnished with truffle pate, brandy and with a fried egg
on top and spam."}'
>>>

Or to dump to a file:

>>> with open("spam.json", "w") as fp:
...     sdjson.dumps(class_instance, fp)
...
>>>

sdjson also provides access to load, loads, JSONDecoder, JSONDecodeError, and JSONEncoder from the json module, allowing you to use sdjson as a drop-in replacement for json.

If you wish to dump an object without using the custom encoders, you can pass a different JSONEncoder subclass, or indeed JSONEncoder itself to get the stock functionality.

>>> sdjson.dumps(class_instance, cls=sdjson.JSONEncoder)
>>>

When you’ve finished, if you want to unregister the encoder you can call:

>>> sdjson.encoders.unregister(MyClass)
>>>

to remove the encoder for MyClass. If you want to replace the encoder with a different one it is not necessary to call this function: the @sdjson.encoders.register decorator will replace any existing decorator for the given class.

Note that this module cannot be used to create custom encoders for any object json already knows about; that is: dict, list, tuple, str, int, float, bool, and None.

TODO

  1. Add support for custom decoders.

Metadata

Release files for sdjson 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sdjson 0.5.0
File Size Uploaded
sdjson-0.5.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sdjson 0.5.0
File Interpreter ABI Platform
sdjson-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.4 kB

Release files / sdjson-0.5.0.tar.gz

Download URL sdjson-0.5.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1cb9fc9316b2fab479575f9a3102190da49f151be25a87e296a2316707b9e46e
BLAKE2b-256 checksum
How to use checksums
1f37e4688b0d818e43809c8587aff64c1c940310a69eb7d6164dc604d5349ddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release files / sdjson-0.5.0-py3-none-any.whl

Download URL sdjson-0.5.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e31c8075ec65671668dfaa065267fe565790aeee5d31f44b9c1408c57d8054c
BLAKE2b-256 checksum
How to use checksums
d40a086439278bf5aac9c9706bdaba4e1a68cd903b90da479c0aa421eb1c92a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page