[Python-ideas] Json object-level serializer

Ronald Oussoren ronaldoussoren at mac.com
Thu Jul 29 21:13:25 CEST 2010


On 29 Jul, 2010, at 17:28, Alexander Belopolsky wrote:

> On Thu, Jul 29, 2010 at 7:54 AM, M.-A. Lemburg <mal at egenix.com> wrote:
>> Antoine Pitrou wrote:
>>> On Thu, 29 Jul 2010 13:35:41 +0200
>>> Tarek Ziadé <ziade.tarek at gmail.com> wrote:
>>>> Hello,
>>>> 
>>>> What about adding in the json package the ability for an object to
>>>> provide a different object to serialize ?
>>>> This would be useful to translate a class into a structure that can be
>>>> passed to json.dumps
>>> 
>>> How about letting json use the __reduce__ protocol instead?
>> 
> 
> +1.  I  think this is a very sensible idea.  Note that Tarek's request
> was not for a magic method like __repr__ that would return an easy to
> parse string.  Instead, the request was for a method that would return
> an object that can be serialized instead of the original object and
> will carry enough data to restore the original object.

I'm -1 on this because the __reduce__ protocol and the proposed
__json__ protocol have slightly different purposes.  When I use JSON
I generally only publish part of the object-state into JSON, even when
pickling the object would store the entire state.

Another reason for not sharing the same method for pickling and json
serialisation is that the json side may have external constraints (that is,
the consumer of the JSON data may have requirements on how objects
are serialized) and those constraints should not limit how the object
can be pickled.

Ronald
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3567 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100729/f4a9541d/attachment.bin>


More information about the Python-ideas mailing list