[Python-ideas] Json object-level serializer

M.-A. Lemburg mal at egenix.com
Thu Jul 29 14:27:26 CEST 2010


Tarek Ziadé wrote:
> On Thu, Jul 29, 2010 at 1:54 PM, 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?
>>
>> How would you then write a class that works with both pickle
>> and json ?
>>
>> IMO, we'd need a separate method to return a JSON version of
>> the object, e.g. .__json__(). I'm not sure how deserialization
>> could be handled, since JSON doesn't support arbitrary object
>> types.
> 
> As I told Oleg, I think its OK not to have a round trip like Pickle.
> 
> The use case I have is to express a structure in Json, but loading it back
> can be done in a custom, explicit process.
> 
> It cannot be triggered from the json package itself since it cannot know
> that a given Json structure was built through a specific class.

I just wanted to emphasize that a separate new
method is needed, rather than trying to reuse a pickle-protocol
method.

I don't think deserialization support is needed either. The application
getting the decoded JSON data can do that in an application specific
way based on the lists and dictionaries it gets from the JSON decoder.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jul 29 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-ideas mailing list