On 29 Jul, 2010, at 17:28, Alexander Belopolsky wrote:
On Thu, Jul 29, 2010 at 7:54 AM, M.-A. Lemburg firstname.lastname@example.org wrote:
Antoine Pitrou wrote:
On Thu, 29 Jul 2010 13:35:41 +0200 Tarek Ziadé email@example.com wrote:
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.