Forking simplejson

Nathan Rice nathan.alexander.rice at gmail.com
Wed Oct 26 13:34:04 EDT 2011


Since this happily went off to the wrong recipient the first time...

The python json module/simpljson are badly in need of an architecture
update.  The fact that you can't override the encode method of
JSONEncoder and have it work reliably without monkey patching the pure
python encoder is a sign that something is horribly wrong.

On Wed, Oct 26, 2011 at 5:14 AM, Amirouche Boubekki
<amirouche.boubekki at gmail.com> wrote:
> Héllo,
>
> I would like to fork simplejson [1] and implement serialization rules based
> on protocols instead of types [2], plus special cases for protocol free
> objects, that breaks compatibility. The benefit will be a better API for
> json serialization of custom classes and in the case of iterable it will
> avoid a calls like:
>
>>>> simplejson.dumps(list(my_iterable))
>
> The serialization of custom objects is documented in the class instead of
> the ``default`` function of current simplejson implementation [3].
>
> The encoding algorithm works with a priority list that is summarized in the
> next table:
>
>     +-------------------+---------------+
>     | Python protocol   | JSON          |
>
>
>     |  or special case  |               |
>     +===================+===============+
>     | (ø) __json__      | see (ø)       |
>
>
>     +-------------------+---------------|
>
>     | map               | object        |
>
>
>     +-------------------+---------------+
>     | iterable          | array         |
>     +-------------------+---------------+
>     | (*) float,int,long| number        |
>
>
>     +-------------------+---------------+
>     | (*) True          | true          |
>     +-------------------+---------------+
>     | (*) False         | false         |
>
>
>     +-------------------+---------------+
>     | (*) None          | null          |
>     +-------------------+---------------+
>     | (§) unicode       | see (§)       |
>
>
>     +-------------------+---------------+
>
> (ø) if the object implements a __json__ method, the returned value is used
> as the serialization of the object
>
>
> (*) special objects which are protocol free are serialized the same way it's
> done currently in simplejson
> (§) if the algorithm arrives here, call unicode (with proper encoding rule)
> on the object and use the result as json serialization
>
>
> As soon as an object match a rule, it's serialized.
>
> What do you think ? Do you find this API an improvement over simplejson ? Is
> it worth to code ?
>
> Where are documented the different protocols implemented by Python objects ?
>
>
>
> Regards,
>
> Amirouche
>
> [1] https://github.com/simplejson/simplejson
> [2]
> https://github.com/simplejson/simplejson/blob/master/simplejson/encoder.py#L75
> [3]
> http://simplejson.readthedocs.org/en/latest/index.html#simplejson.JSONEncoder.default
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



More information about the Python-list mailing list