Héllo,<br><br>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:<br>
<br>>>> simplejson.dumps(list(my_iterable))<br><br>The serialization of custom objects is documented in the class instead of the ``default`` function of current simplejson implementation [3].<br><br>The encoding algorithm works with a priority list that is summarized in the next table:<br>
<br><pre><div style="background-color: transparent;" class="line" id="LC80"><span class="sd"> +-------------------+---------------+</span></div><div style="background-color: transparent;" class="line" id="LC81"><span class="sd"> | Python protocol | JSON |<br>
| or special case | |<br></span></div><div style="background-color: transparent;" class="line" id="LC82"><span class="sd"> +===================+===============+</span><br> | (ø) __json__ | see (ø) |<br>
+-------------------+---------------|<br></div><div style="background-color: transparent;" class="line" id="LC83"><pre><span class="sd"> </span><span class="sd"> | map | object |</span><span class="sd"><br>
+-------------------+---------------+</span><span class="sd"><br> | iterable | array |</span><br> +-------------------+---------------+<span class="sd"><br> | (*) float,int,long| number |</span><span class="sd"><br>
+-------------------+---------------+</span><span class="sd"><br> | (*) True | true |</span><span class="sd"><br> +-------------------+---------------+</span><span class="sd"><br> | (*) False | false |</span><span class="sd"><br>
+-------------------+---------------+</span><span class="sd"><br> | (*) None | null |</span><br> +-------------------+---------------+<span class="sd"><br> </span>| (§) unicode | see (§) |<span class="sd"><br>
+-------------------+---------------+</span><span class="sd"></span><span class="sd"></span><br></pre></div><div style="background-color: transparent;" class="line" id="LC96"><br>(ø) if the object implements a __json__ method, the returned value is used as the serialization of the object<br>
(*) special objects which are protocol free are serialized the same way it's done currently in simplejson<br>(§) if the algorithm arrives here, call unicode (with proper encoding rule) on the object and use the result as json serialization<br>
</div></pre>As soon as an object match a rule, it's serialized.<br><br>What do you think ? Do you find this API an improvement over simplejson ? Is it worth to code ?<br><br>Where are documented the different protocols implemented by Python objects ?<br>
<br><br><br>Regards,<br><br>Amirouche<br><br>[1] <a href="https://github.com/simplejson/simplejson">https://github.com/simplejson/simplejson</a><br>[2] <a href="https://github.com/simplejson/simplejson/blob/master/simplejson/encoder.py#L75">https://github.com/simplejson/simplejson/blob/master/simplejson/encoder.py#L75</a><br>
[3] <a href="http://simplejson.readthedocs.org/en/latest/index.html#simplejson.JSONEncoder.default">http://simplejson.readthedocs.org/en/latest/index.html#simplejson.JSONEncoder.default</a><br>