Configurable JSON Encoder
By default, the json.Encoder class' logic can only be customized to a certain extent:
- a 'default' method can be overwritten to encode objects that JSON can't serialize by default;
- an 'encode' method can be overwritten to fully rewrite the encoding logic, which is powerful but can be hard to change if you need to change the specific behavior for a type in a nested structure;
- an 'iterencode' method can be overwritten to handle the recursive behavior of the JSON encoder, but this method uses
functions under the hood that are not defined as class methods but rather as functions in the encoder
module (
c_make_encoderand_make_iterencode), which thus can not be overwritten by class inheritance.
This module aims to help make adding custom rules to the JSON encoder easier, by injecting a middle
layer ConfigurableJsonEncoder which essentially uses a copied and slightly modified version of _make_iterencode from
the original module.
Some examples of things you can do with this module (see examples.py for the actual rules)
from examples import *
data = {
'original': ['behavior'],
'set': {1, 2, 3},
'namedtuple': Letters('a', 'b', 'c'),
"class": MyClass()
}
try:
print(json.dumps(data))
except TypeError:
... # Object of type set is not JSON serializable
# default behavior without type error:
print(json.dumps(data, default=str))
# {"original": ["behavior"], "set": "{1, 2, 3}", "namedtuple": ["a", "b", "c"], "class": "<__main__.MyClass object at 0x...>"}
# the same behavior as above
print(json.dumps(data, cls=DummyEncoder))
# {"original": ["behavior"], "set": "{1, 2, 3}", "namedtuple": ["a", "b", "c"], "class": "<__main__.MyClass object at 0x...>"}
# encodes set into a list:
print(json.dumps(data, cls=SetEncoder))
# {"original": ["behavior"], "set": [1, 2, 3], "namedtuple": ["a", "b", "c"], "class": "<__main__.MyClass object at 0x...>"}
# calls .tojson() which uses transform to output a string
print(json.dumps(data, cls=ToJSONEncoder))
# {"original": ["behavior"], "set": "{1, 2, 3}", "namedtuple": ["a", "b", "c"], "class": ["my", "data", "as", "json"]}
# converts namedtuple to a dictionary instead of a list (the default behavior)
print(json.dumps(data, cls=MyEncoder))
# {"original": ["behavior"], "set": [1, 2, 3], "namedtuple": {"a": "a", "b": "b", "c": "c"}, "class": {"my data": ["as", "a", "dict"]}}
Usage:
Simply extend ConfigurableJsonEncoder with a rules method that returns a JSONRule or None based on the
input o (which is one unit in the nested data tree). Again, see examples.py for inspiration.
Metadata
Release files for Configurable-JSON 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| configurable_json-1.1.0.tar.gz | 7.4 kB | Without provenance | Details for configurable_json-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| configurable_json-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for configurable_json-1.1.0-py3-none-any.whl |
Total release size: 14.1 kB
Release files / configurable_json-1.1.0.tar.gz
| Download URL | configurable_json-1.1.0.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8f6122c6e57afb816b13aeec883bd779d30cb1eb6daed5fdc2fd34f344baabf3
|
|
BLAKE2b-256 checksum How to use checksums |
d45f62f70e9ddb41227c4f722b1654730628028236ca8abe413249a2e8fe4bb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.24.1
|
Release files / configurable_json-1.1.0-py3-none-any.whl
| Download URL | configurable_json-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da3cc918da63e76486dd08ed245ea7345cf318ec5babb056113222dbab5607fb
|
|
BLAKE2b-256 checksum How to use checksums |
6174587f36038da4bce7d9341e1eba2244d4c9b95fe295b8fe220f8fa4c0f77c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.24.1
|
Security for Configurable-JSON 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.