jsonext-0.4.1/0000700000175000017500000000000012313401063013053 5ustar marcmarc00000000000000jsonext-0.4.1/setup.cfg0000600000175000017500000000007312313401063014676 0ustar marcmarc00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 jsonext-0.4.1/PKG-INFO0000600000175000017500000000212312313401063014150 0ustar marcmarc00000000000000Metadata-Version: 1.0 Name: jsonext Version: 0.4.1 Summary: Well-structured helpers to help serializing commonly encountered structures to JSON (like datetime, to_dict(), etc. Home-page: http://github.com/mbr/jsonext Author: Marc Brinkmann Author-email: git@marcbrinkmann.de License: MIT Description: jsonext ======= .. image:: https://travis-ci.org/mbr/jsonext.svg?branch=master :target: https://travis-ci.org/mbr/jsonext jsonext makes it easy to serialize objects outside of the standard Python primitives to JSON:: >>> import jsonext >>> from datetime import datetime >>> jsonext.dumps(datetime.now()) '"2014-03-22T22:17:18.304528+00:00"' >>> jsonext.dumps(i**2 for i in range(10)) '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' It uses mixins to the standard encoder to achieve this and is easily reuse- and extensible. Check out the `documentation `_ for details. Platform: UNKNOWN jsonext-0.4.1/setup.py0000600000175000017500000000123412313401062014566 0ustar marcmarc00000000000000#!/usr/bin/env python # -*- coding: utf-8 -*- import os from setuptools import setup, find_packages def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() setup( name='jsonext', version='0.4.1', description='Well-structured helpers to help serializing commonly ' 'encountered structures to JSON (like datetime, to_dict(), ' ' etc.', long_description=read('README.rst'), author='Marc Brinkmann', author_email='git@marcbrinkmann.de', url='http://github.com/mbr/jsonext', license='MIT', packages=find_packages(exclude=['tests']), install_requires=['times'], ) jsonext-0.4.1/README.rst0000600000175000017500000000116212313401062014543 0ustar marcmarc00000000000000jsonext ======= .. image:: https://travis-ci.org/mbr/jsonext.svg?branch=master :target: https://travis-ci.org/mbr/jsonext jsonext makes it easy to serialize objects outside of the standard Python primitives to JSON:: >>> import jsonext >>> from datetime import datetime >>> jsonext.dumps(datetime.now()) '"2014-03-22T22:17:18.304528+00:00"' >>> jsonext.dumps(i**2 for i in range(10)) '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' It uses mixins to the standard encoder to achieve this and is easily reuse- and extensible. Check out the `documentation `_ for details. jsonext-0.4.1/jsonext.egg-info/0000700000175000017500000000000012313401063016237 5ustar marcmarc00000000000000jsonext-0.4.1/jsonext.egg-info/top_level.txt0000600000175000017500000000001012313401063020762 0ustar marcmarc00000000000000jsonext jsonext-0.4.1/jsonext.egg-info/requires.txt0000600000175000017500000000000512313401063020634 0ustar marcmarc00000000000000timesjsonext-0.4.1/jsonext.egg-info/dependency_links.txt0000600000175000017500000000000112313401063022307 0ustar marcmarc00000000000000 jsonext-0.4.1/jsonext.egg-info/SOURCES.txt0000600000175000017500000000032312313401063020123 0ustar marcmarc00000000000000README.rst setup.py jsonext/__init__.py jsonext/mixins.py jsonext.egg-info/PKG-INFO jsonext.egg-info/SOURCES.txt jsonext.egg-info/dependency_links.txt jsonext.egg-info/requires.txt jsonext.egg-info/top_level.txtjsonext-0.4.1/jsonext.egg-info/PKG-INFO0000600000175000017500000000212312313401063017334 0ustar marcmarc00000000000000Metadata-Version: 1.0 Name: jsonext Version: 0.4.1 Summary: Well-structured helpers to help serializing commonly encountered structures to JSON (like datetime, to_dict(), etc. Home-page: http://github.com/mbr/jsonext Author: Marc Brinkmann Author-email: git@marcbrinkmann.de License: MIT Description: jsonext ======= .. image:: https://travis-ci.org/mbr/jsonext.svg?branch=master :target: https://travis-ci.org/mbr/jsonext jsonext makes it easy to serialize objects outside of the standard Python primitives to JSON:: >>> import jsonext >>> from datetime import datetime >>> jsonext.dumps(datetime.now()) '"2014-03-22T22:17:18.304528+00:00"' >>> jsonext.dumps(i**2 for i in range(10)) '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' It uses mixins to the standard encoder to achieve this and is easily reuse- and extensible. Check out the `documentation `_ for details. Platform: UNKNOWN jsonext-0.4.1/jsonext/0000700000175000017500000000000012313401063014545 5ustar marcmarc00000000000000jsonext-0.4.1/jsonext/mixins.py0000600000175000017500000000541512313401062016434 0ustar marcmarc00000000000000import datetime import times class JSONDateTimeMixin(object): """A mixin for JSONEncoders, encoding :class:`datetime.datetime` and :class:`datetime.date` objects by converting them to strings that can be parsed by all modern browsers JS Date() object. All timestamps are converted to UTC before being serialized. Date objects simply use :meth:`~datetime.date.isoformat`. >>> import jsonext >>> from datetime import datetime >>> dt = datetime(2013, 11, 17, 12, 00, 00) # Python 3.3.3 release! >>> jsonext.dumps(dt) '"2013-11-17T12:00:00+00:00"' >>> d = dt.date() >>> d datetime.date(2013, 11, 17) >>> jsonext.dumps(d) '"2013-11-17"' """ def default(self, o): if isinstance(o, datetime.datetime): return times.format(o, 'Zulu') if isinstance(o, datetime.date): return o.isoformat() return super(JSONDateTimeMixin, self).default(o) class JSONIterableMixin(object): """A mixin for JSONEncoders, encoding any iterable type by converting it to a list. Especially useful for SQLAlchemy results that look a lot like regular lists or iterators, but will trip up the encoder. Beware of infinite generators. >>> import jsonext >>> gen = (i**2 for i in range(10)) >>> jsonext.dumps(gen) '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' """ def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) return super(JSONIterableMixin, self).default(o) class JSONToDictMixin(object): """A mixin for JSONEncoders, encoding any object with a to_dict() method by calling that method and encoding the return value. >>> import jsonext >>> class Foo(object): ... def __init__(self, a, b): ... self.a = a ... self.b = b ... def to_dict(self): ... return {'A': self.a, 'B': self.b} ... >>> items = [Foo(1,2), Foo(3,4)] >>> jsonext.dumps(items) '[{"A": 1, "B": 2}, {"A": 3, "B": 4}]' """ def default(self, o): if hasattr(o, 'to_dict'): return o.to_dict() return super(JSONToDictMixin, self).default(o) class JSONStringifyMixin(object): """A mixing for JSONEncoders, encoding any object that has a ``__str__`` method with the return value of said function. >>> import jsonext >>> from decimal import Decimal as D >>> x = D('123.456') >>> jsonext.dumps(x) '"123.456"' >>> from datetime import timedelta >>> t = timedelta(days=5, seconds=12345) >>> jsonext.dumps(t) '"5 days, 3:25:45"' """ def default(self, o): if hasattr(o, '__str__'): return str(o) return super(JSONStringifyMixin, self).default(o) jsonext-0.4.1/jsonext/__init__.py0000600000175000017500000000114112313401062016654 0ustar marcmarc00000000000000"""JSON-rendering helpers. This module provides mixins for the stdlib :class:`json.JSONEncoder` class, adding serialization methods for other object types, such as :class:`~datetime.datetime` objects or iterables. All these are ready to use by using :data:`~jsonext.dumps`. """ import functools import json from .mixins import JSONDateTimeMixin, JSONIterableMixin, JSONToDictMixin, \ JSONStringifyMixin class JSONEncoder(JSONDateTimeMixin, JSONIterableMixin, JSONToDictMixin, JSONStringifyMixin, json.JSONEncoder): pass dumps = functools.partial(json.dumps, cls=JSONEncoder)