Skip to main content

Help composing objects to build JSON.

Project description

Help composing objects to emit JSON.

emitjson.repository() creates a standard single-dispatch generic function to covert objects to JSON-friendly objects. repository() converts some object types that are not supported by json module such as set() or datetime.datetime().

You can also add your own converters as singledispatch function.

import emitjson
import base64

my_repo = emitjson.repository()  # create emitjson repository

@myrepo.register(bytes)
def conv_bytes(obj):
    # encode bytes object in Base64 format
    return base64.b64encode(obj).decode('ascii')

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

@my_repo.register(Class1)
class Class1Converter(ObjConverter):
    prop1 = attr    # get obj.prop1
    prop2 = attr    # get obj.prop2

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

Requirements

  • Python 3.4 or later

Functions

@emitjson.repository

Returns a single-dispatch generic function instance.

emitjson.repository() overrides following types by default.

  • collections.abc.Sequence objects are converted to tuple. Elements in the sequence are converted recursively.

  • Keys and values of the collections.abc.Mapping are converted recursively.

  • datetime.date and datetime.datetime objects are converted to isoformat() string.

emitjson.ObjConverter class

emitjson.ObjConverter class defines a converter from arbitrary objects to dictionary.

You can register field names of the target object. If an attribute of the ObjConverter is emitjson.attr class, converter gets a value from attribute in the same name from the target object. If an attribute is an instance of attr class, converter gets a value from the attribute specified in the attr object.

emitjson.attr class

Specify name of attribute to get value. The attribute name to get value could be supplied as arguments of attr. See above for examples.

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

Resulting dict objects are also converted recursively.

Project details


Download files

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

Source Distribution

emitjson-0.0.1.tar.gz (3.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page