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.

Project description

Build Status Documentation Status PyPI PyPI - Python Version Coverage PyPI - License

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.

Based on https://treyhunner.com/2013/09/singledispatch-json-serializer/ and Python’s json module.


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)
>>>

Note: This module does not currently support custom decoders, but might in the future.

TODO

  1. Write tests with pytest. Add appropriate sections to .travis.yml and setup.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sdjson-0.1.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

sdjson-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file sdjson-0.1.1.tar.gz.

File metadata

  • Download URL: sdjson-0.1.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.7

File hashes

Hashes for sdjson-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cfd0a15f292e3a5d6119faabe2522e98ed054e5768e01577f02829802ecfcbdc
MD5 c164852d174ba34b7d52cb67b692cdef
BLAKE2b-256 4a023c44d9cd0399fa7d0c8ac1f0f5c4f0d1fa12477f2ab7e505609c68fe6616

See more details on using hashes here.

File details

Details for the file sdjson-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sdjson-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.7

File hashes

Hashes for sdjson-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 12fe1a351484e333fabbc95132b840c51a32e692f75e78e1e531056692ad24cd
MD5 346db555def828fa8506e1fe38afdff8
BLAKE2b-256 34a65565b2d19f7505736032cb5714e94657c3c83608a3c933128cd1fbbcb2eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page