Using Python simplejson to return pregenerated json

I have a GeoDjango model object that I do not want to serialize for json. I do it in my opinion:

lat = float(request.GET.get('lat'))
lng = float(request.GET.get('lng'))
a = Authority.objects.get(area__contains=Point(lng, lat))
if a:
    return HttpResponse(simplejson.dumps({'name': a.name, 
                                          'area': a.area.geojson,
                                          'id': a.id}), 
                        mimetype='application/json')

The problem is that simplejsontreats a.area.geojson as a simple string, even if it is a beautifully pre-generated json. It is easily fixed in the client eval()in the area, but I would like to do it correctly. Can I say simplejsonthat the specific string is already json and should be used as-is (and not returned as a simple string)? Or is there another workaround?

UPDATE To clarify, this is json, which is currently being returned:

{
    "id": 95,
    "name": "Roskilde",
    "area": "{ \"type\": \"MultiPolygon\", \"coordinates\": [ [ [ [ 12.078701, 55.649927 ], ... ] ] ] }"
}

The challenge is to "area" be a json dictionary instead of a simple string.

+5
2

EDITED :

- :

lat = float(request.GET.get('lat'))
lng = float(request.GET.get('lng'))
a = Authority.objects.get(area__contains=Point(lng, lat))
if a:
    json = simplejson.dumps({'name': a.name, 
                             'area': "{replaceme}",
                             'id': a.id}), 
    return HttpResponse(json.replace('"{replaceme}"', a.area.geojson),
                        mimetype='application/json')
+2

, - JSONEncoder , , JSON. - . , JSONEncoder .

class SkipJSONEncoder(simplejson.JSONEncoder):
     def default(self, obj):
         if isinstance(obj, str) and (obj[0]=='{') and (obj[-1]=='}'): 
             return obj
         return simplejson.JSONEncoder.default(self, obj)

, , :

simplejson.dumps(..., cls=SkipJSONEncoder)

, - JSON, ( - , '{' end in '}', ).

+5

All Articles