[Python-ideas] Json object-level serializer

Daniel Fetchinson fetchinson at googlemail.com
Thu Jul 29 14:47:34 CEST 2010


> 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
>
> So, it __json__ is provided, its used for serialization instead of the
> object itself:
>
>>>> import json
>>>> class MyComplexClass(object):
> ...     def __json__(self):
> ...         return 'json'
> ...
>>>> o = MyComplexClass()
>>>> json.dumps(o)
> '"json"'

Have a look at turbojson [1], the jsonification package that uses
peak.rules [2] and which comes with turbogears [3]. It does exactly
what you propose.

Cheers,
Daniel

[1a] http://pypi.python.org/pypi/TurboJson
[1b] http://svn.turbogears.org/projects/TurboJson
[2a] pypi.python.org/pypi/PEAK-Rules
[2b] http://peak.telecommunity.com/DevCenter/RulesReadme
[3] http:///www.turbogears.org


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-ideas mailing list