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
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.
Docs |
|
---|---|
Tests |
|
PyPI |
|
Anaconda |
|
Activity |
|
Other |
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
Add support for custom decoders.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sdjson-0.2.6.tar.gz
.
File metadata
- Download URL: sdjson-0.2.6.tar.gz
- Upload date:
- Size: 39.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89540860429527fbe41b56e0c7ec5897edbd343bffcc631b4f7068359a1d8eb6 |
|
MD5 | 71637b4aa8e1b9b4ad85277ba841372a |
|
BLAKE2b-256 | 4a85733bb0c0c2cdf1b25afba9eb516746346ee6e27d48a094d7e4b92dac9d2c |
File details
Details for the file sdjson-0.2.6-py3-none-any.whl
.
File metadata
- Download URL: sdjson-0.2.6-py3-none-any.whl
- Upload date:
- Size: 56.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 613679dd000a507f78c01dada999fb7db988203d4bafe1356ed69eb7c5abab87 |
|
MD5 | 398f2bc823b4ca0ab9cd1fea472e54ec |
|
BLAKE2b-256 | 2cf73eb3f3a1647c3d5adf9592ba2ceff37f7ba87bf0e2bafa38bcbc1035a462 |