Forking simplejson
Amirouche Boubekki
amirouche.boubekki at gmail.com
Wed Oct 26 05:14:02 EDT 2011
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111026/b9df8b0a/attachment.html>
More information about the Python-list
mailing list