Skip to main content

Help composing objects to build JSON.

Project description

Help composing objects to emit JSON.

emitjson.repository() creates a repogistory of functions to convert various type of objects to JSON-friendly objects. The repository is a single-dispatch generic function that returns a converted object if the converter for the type is registered.

The repository contains some default converters for types that are not supported by json module such as set() or datetime.datetime(). Also, default repository returns (shallow) copies of standard container types such as or Items in the container object are also converted recursively.

>>> import datetime
>>> import emitjson
>>> my_repo = emitjson.repository()  # create emitjson repository
>>> src = [1, 2,, {4, 5, 6}]
>>> new = my_repo(src)
>>> print(new)
(1, 2, '2016-04-20T22:09:18.731157', (4, 5, 6))

You can add your own converters as singledispatch function. Following example add a converter to generate Base64 strings from bytes objects.

>>> import emitjson
>>> import base64
>>> my_repo = emitjson.repository()  # create emitjson repository
>>> @my_repo.register(bytes)
>>> def conv_bytes(obj):
...    # encode bytes object in Base64 format
...    return base64.b64encode(obj).decode('ascii')
>>> print(my_repo([b'abcd']))
('YWJjZA==', )

You can also define mapper class to convert objects to dictionary.

from emitjson repository, attr, ObjConverter

class Class1:
    def __init__(self):
        self.prop1 = 'spam'
        self.prop2 = 'ham'

class Class2:
    def __init__(self):
        self.prop3 = [Class1()]

my_repo = emitjson.repository()  # create emitjson repository

class Class1Converter(ObjConverter):
    prop1 = attr    # get obj.prop1
    prop2 = attr    # get obj.prop2

class Class2Converter(ObjConverter):
    prop_x = attr('prop3')  # get obj.prop3 but store as 'prop_x'


  • Python 3.4 or later



Create a repository of object converters. The repository is an instance of the single-dispatch generic function.

The repository overrides following types by default.

  • objects are converted to tuple. Elements in the sequence are converted recursively.
  • Keys and values of the are converted recursively.
  • and datetime.datetime objects are converted to isoformat() string.

ObjConverter class

ObjConverter class create a converter from an arbitrary object to a dictionary. Values obtained from objects are also converted recursively.

Attribute names to be converted are defined as attr class members of ObjConverter class.

If an attribute of the ObjConverter is an instance of attr, attrname arguments specifies attribute name to be converted.

Resulting dict objects are also converted recursively.

attr(attrname=None, map=None) class

Converts an attribute of object.

attrname overrides the attribute name if not omitted.

map argument which is a functon to convert a target object to arbitrary objects.

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 emitjson, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size emitjson-0.0.2.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page