Skip to main content

Simplify JSON Encoders and Object Hooks

Project description

Build Status Coverage Status


Simplifies building custom encoders and object-hooks for Python’s default JSON implementation.


$ pip install json-object-factory


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


Or use the included decorators:

class MyOtherJsonHandler(object):

def an_encoder_function(o):

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)


  • 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

Project details

Download files

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

Files for json-object-factory, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size json_object_factory-0.0.2-py2.py3-none-any.whl (5.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size json-object-factory-0.0.2.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page