Package: emitjson
Help composing objects to emit JSON.emitjson.repository()creates a repository of functions to convert various type of objects to JSON-friendly objects. The repository is asingle-dispatch generic functionthat returns a converted object if the converter for the type is registered.The repository contains some default converters for types that are not supported byjson modulesuch asset()ordatetime.datetime(). Also, default repository returns (shallow) copies of standard container types such ascollections.abc.Mappingorcollection.abc.Sequence. Items in the container object are also converted recursively.>>> import datetime
>>> import emitjson
>>> my_repo = emitjson.repository()  # create emitjson repository
>>> src = [1, 2, datetime.datetime.now(), {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 assingledispatch function. Following example add a converter to generate Base64 strings frombytesobjects.>>> 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

@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'RequirementsPython 3.4 or laterFunctions@repository()Create a repository of object converters. The repository is an instance of thesingle-dispatch generic function.The repository overrides following types by default.collections.abc.Sequenceobjects are converted totuple. Elements in the sequence are converted recursively.Keys and values of thecollections.abc.Mappingare converted recursively.datetime.dateanddatetime.datetimeobjects are converted toisoformat()string.ObjConverter classObjConverterclass 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 asattrclass members ofObjConverterclass.If an attribute of theObjConverteris an instance ofattr,attrnamearguments specifies attribute name to be converted.Resulting dict objects are also converted recursively.ObjConverter.on_convert(obj, values)on_convertmethod is called to obtain attribute ofobjand store the value to thevaluesdictionary. You can overrideon_convert()method to customize conversion.class Class3:
    prop = 1

@myrepo.register(Class3)
class Class3Converter(ObjConverter):
    def on_convert(self, obj, values):
        super().on_convert(obj, values)
        values['dynamic_prop'] = obj.prop

print(myrepo(Class3())  # prints {'dynamic_prop': 1}attr(attrname=None, map=None) classConverts an attribute of object.attrnameoverrides the attribute name if not omitted.mapargument is a functon to convert a target object to arbitrary objects.SAModelConverter classSAModelConverterclass create a converter from a SQLAlchemy model. Columns of the model are converted unless specified inIGNORESmember of the class.class Test(Base):
    __tablename__ = 'test'
    a = Column(Integer, primary_key=True)
    b = Column(Integer)
    c = Column(Integer)
    d = Column(Integer)

@myrepo.register(Test)
class TestModelConverter(SAModelConverter):
    IGNORES = ('c', 'd')    # ommit Test.c and Test.drepository.fromSQLAlchemyModel(model, attrs=None, ignores=None)Another way to register a converter of SQLAlchemy model. Columns of the model are converted unless the name of column is listed inignoreargument.attrsis a dictionary of key name andattrobject.# Converts Test model
myrepo.fromSQLAlchemyModel(Test,
    attrs={
        'fld1': attr,           # Emits Test.fld1 as value of 'fld1'
        'X_VALUE':attr('fld_X') # Emits Test.fld_X as value of 'X_VALUE'
    },
    ignores=('fld3', 'fld4')) # ignore Test.fld3 and Test.fld2DjangoModelConverter classDjangoModelConverterclass create a converter from a Django model. Columns of the model are converted unless specified inIGNORESmember of the class.class DjangoModel(models.Model):
    charattr = models.CharField(max_length=10)
    intattr = models.IntegerField()

@myrepo.register(DjangoModel)
class TestModelConverter(DjangoModelConverter):
    passrepository.fromSQLAlchemyModel(model, attrs=None, ignores=None)Another way to register a converter of Django model. Columns of the model are converted unless the name of column is listed inignoreargument.attrsis a dictionary of key name andattrobject.# Converts Test model
myrepo.fromDjangoModel(Test,
    attrs={
        'fld1': attr,           # Emits Test.fld1 as value of 'fld1'
        'X_VALUE':attr('fld_X') # Emits Test.fld_X as value of 'X_VALUE'
    },
    ignores=('fld3', 'fld4')) # ignore Test.fld3 and Test.fld2CopyrightCopyright (c) 2016 Atsuo IshimotoPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.