[Python-ideas] Json object-level serializer (original) (raw)
M.-A. Lemburg mal at egenix.com
Thu Jul 29 23:25:09 CEST 2010
- Previous message: [Python-ideas] Json object-level serializer
- Next message: [Python-ideas] Json object-level serializer
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Georg Brandl wrote:
Am 29.07.2010 14:31, schrieb M.-A. Lemburg:
Georg Brandl wrote:
Am 29.07.2010 13:35, schrieb Tarek Ziadé:
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 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"' You can do this with a very short subclass of the JSONEncoder: class MyJSONEncoder(JSONEncoder): def default(self, obj): return obj.json() # with a useful failure message Does that also work with the JSON C extension ? I think so. The C encoder gets the default function as an argument.
Then that sounds like the right way forward.
-- 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/
- Previous message: [Python-ideas] Json object-level serializer
- Next message: [Python-ideas] Json object-level serializer
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]