Closed GoogleCodeExporter closed 9 years ago
unfortunately, the json output is handled by post-processing the xml. this was
a convenient and easy way to add json support, but, as a consequence, any
number information is unavailable at the time the json output is generated. as
such, it's unlikely that this will be changed.
Original comment by jahlborn@gmail.com
on 18 Jun 2011 at 12:28
I had a similar problem. I usually don't use xml for any of my web stuff, and I
also wanted to represent the datatype as much as possible. Writing your own
Model->Rest functions is an easy and quick way to do this.
Here is a sample of what I am doing
-------------------------------
import simplejson as json
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, 'isoformat'): #handles both date and datetime objects
return obj.isoformat()
else:
return json.JSONEncoder.default(self, obj)
def toJSON(_object, default):
result = None
if isinstance(_object, list):
result = []
for item in _object:
_result = dict([(p, getattr(item, p)) for p in item.properties()])
_result['key'] = item.key().id_or_name()
result.append(_result)
else:
try:
result = dict([(p, getattr(_object, p)) for p in _object.properties()])
result['key'] = _object.key().id_or_name()
except:
LOG.exception("Error trying to convert %r to json", _object)
result = default
return json.dumps(result, cls=JSONEncoder)
--------------------
The above function toJSON will work with Model Objects, and the json string
will have proper (almost) datatypes.
Original comment by dshe...@etsy.com
on 12 Dec 2011 at 9:49
think i should be able to make this happen after all.
Original comment by jahlborn@gmail.com
on 29 Jun 2012 at 4:28
This issue was closed by revision r79.
Original comment by jahlborn@gmail.com
on 30 Jun 2012 at 12:20
Original issue reported on code.google.com by
redrocke...@gmail.com
on 17 Jun 2011 at 9:37