json-object-factory
Simplifies building custom encoders and object-hooks for Python’s default JSON implementation.
Installation
$ pip install json-object-factory
Usage
Add encoders and decoders to the registry:
import jsonfactory
class MyJsonHandler(object):
def encode(self, o):
if isinstance(o, MyCustomClass):
return o.serialize()
return None
def decode(self, d):
if 'some_custom_key' in d:
return MyCustomClass(**d)
return d
jsonfactory.Registry.register(MyJsonHandler)
Or use the included decorators:
@jsonfactory.register
class MyOtherJsonHandler(object):
...
@jsonfactory.encoder
def an_encoder_function(o):
...
@jsonfactory.decoder
def a_decoder_function(d):
...
Then use the module’s dumps and loads functions:
json_str = jsonfactory.dumps(obj_dict, indent=2)
new_obj_dict = jsonfactory.loads(json_str)
Notes
The calling signature for encoder functions follows that of the built-in JSONEncoder with one exception:
If no modifications are needed and the object should be passed to the base encoder’s handler, None should be returned. This differs from the normal method of calling super(MyEncoder, self).default(o) (that would most likely be an error since subclassing JSONEncoder isn’t necessary).
The signature for decoder functions follows the object_hook signature in the built-in implementation
Release files for json-object-factory 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json-object-factory-0.0.2.tar.gz | 2.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json_object_factory-0.0.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 8.3 kB
Release files / json-object-factory-0.0.2.tar.gz
| Download URL | json-object-factory-0.0.2.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73eecb07706d511ba2616851a19f229cc957cc1a426e285bce4686e557bd6af8
|
|
BLAKE2b-256 checksum How to use checksums |
96a838e15e5bc446c4afabe422c3e9c7ecad7402f446a2ea6d1b7f4dd83b0425
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / json_object_factory-0.0.2-py2.py3-none-any.whl
| Download URL | json_object_factory-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3f6189fadce539dfd63ff975021fb9a00ba4a3f64f55a328c0119bbf45b28b5e
|
|
BLAKE2b-256 checksum How to use checksums |
d2a63aa1d4604fb871198c3e8b214d7f89d839eed6de0313622aec483e6b2d6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |