Reputation: 6813
Hi I have made a custom JSONEncoder and happened to run into this issue. When I use the dumps
function of simplejson the nested object gets serialize as a string. For example I use this:
simplejson.dumps({'lat': obj.lat, 'lon': obj.lon})
And get this:
{
website: "http://something.org",
location: "{"lat": 12.140158037163658, "lon": -86.24754807669069}"
}
If you see location object is with double quotes is there a way I can specify location object to be dump properly as a JSON object (without the double quotes).
Edit
class jsonEncoder(simplejson.JSONEncoder):
def default(self, obj):
isinstance(obj, db.GeoPt):
return simplejson.dumps({'lat': obj.lat, 'lon': obj.lon})
else:
return simplejson.JSONEncoder.default(self, obj)
Upvotes: 2
Views: 1454
Reputation: 114811
Don't return a string from default() when obj is a db.GeoPt. Instead, return the dict with keys 'lat' and 'lon', and let the JSON library serialize the dict.
Try this:
class jsonEncoder(simplejson.JSONEncoder):
def default(self, obj):
if isinstance(obj, db.GeoPt):
return {'lat': obj.lat, 'lon': obj.lon}
else:
return simplejson.JSONEncoder.default(self, obj)
Upvotes: 4