Cannot JSON serialize python Appengine GeoModel subclass using simple json

1.1k Views Asked by At

I am working on a simple app engine application that utilize the geo-searching proximity_fetch.

I perform the search and some extra filtering on the data, and all goes well (I checked the number of returned results, and it was just as expected ) However, when I tried to dump it into a json using simple json, I got the following error:

TypeError: < backend.models.Listing object at 0x0570CC90 >is not JSON serializable

This is the way I define the object in my code:

class Listing(GeoModel):
    currency=db.IntegerProperty( default = CURRENCY["EURO"] )
    title=db.StringProperty(required = True )
    admins = db.ReferenceProperty( User )
    .
    . Some uninteresting properties goes here, Only Integer and String Properties
    .
    active = db.BooleanProperty()

    @staticmethod
    def get_listing_from_point( lat , lon ,
                            max_dist=MAX_SEARCH_RADIUS,
                            max_result=MAX_SEARCH_RESULT,
                            start_date=None,end_date=None):

        rich_query = Listing.all()
        result = Listing.proximity_fetch( rich_query , 
                                          geo.geotypes.Point( lat , lon ),
                                          max_results = max_result,
                                          max_distance = max_dist )

        result =  list( rich_query )

        valid_list = 
           [i for i in list( result ) if i.check_availability( start_date , 
                                                               end_date )]

        return  valid_list 

And this is the method from which I call the proximity search:

lon = self.request.args.get("lon" , None)
lat = self.request.args.get("lat" , None)
check_in = self.request.args.get("check_in" , None)
check_out = self.request.args.get("check_out" , None)
if not lon or not lat:
   return Response( json.dumps( "{ 'error' : 'desription..." ) )

result = Listing.get_listing_from_point(
                                lat = float(lat),
                                lon = float(lon),
                                start_date = check_in,
                                end_date = check_out)

return json.dumps( result )

While attempting to find the reason for the problem, I tried to return the dict of the first result in the search, and when I did so, I got the following error:

TypeError: datastore_types.GeoPt(23.0, 42.2) is not JSON serializable Which leads me to believe there is something wrong in the way I handle my geo points...

I tried searching for both errors with no result, any clues?

P.S, this is the complete stacktrace:

File "C:\Program >Files\Google\google_appengine\google\appengine\ext\appstats\recording.py", line 897, in >appstats_wsgi_wrapper result = app(environ, appstats_start_response)

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\tipfy\app.py", line 245, in >dispatch rv = self.handle_exception(request, e)

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\tipfy\app.py", line 241, in >dispatch rv = self.router.dispatch(request)

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\tipfy\routing.py", line 105, in >dispatch rv = rv()

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\tipfy\handler.py", line 297, in >call response = self.dispatch()

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\tipfy\handler.py", line 88, in >dispatch return self.handle_exception(exception=e)

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\tipfy\handler.py", line 86, in >dispatch return self.make_response(method(**request.rule_args))

File "C:\Users\roi.SEMANTV\PycharmProjects\gayville_tipfy\backend\handlers.py", line 47, >in get return json.dumps( result )

File "C:\Program >Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson__init__.py", line >182, in dumps **kw).encode(obj)

File "C:\Program >Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson\encoder.py", line >312, in encode chunks = list(self.iterencode(o))

File "C:\Program >Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson\encoder.py", line >262, in _iterencode for chunk in self._iterencode_list(o, markers):

File "C:\Program >Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson\encoder.py", line >170, in _iterencode_list for chunk in self._iterencode(value, markers):

File "C:\Program Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson\encoder.py", line 273, in _iterencode for chunk in self._iterencode_default(o, markers):

File "C:\Program >Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson\encoder.py", line 279, in _iterencode_default newobj = self.default(o)

File "C:\Program >Files\Google\google_appengine\lib\django_0_96\django\utils\simplejson\encoder.py", line >300, in default raise TypeError("%r is not JSON serializable" % (o,))

1

There are 1 best solutions below

0
On BEST ANSWER

Complex Python objects must be representable as a dictionary of simple types in order to serialize to JSON. The "object is not JSON serializable" error basically means that Python's JSON encoder did not know how to serialize your object.

So if result is a GeoPt, you can serialize it like this:

return json.dumps({'lat': result.lat, 'lon': result.lon})