-1
ウェブアプリケーションのAPIを作成しようとしていますが、GeoPtPropertyをJSONにロードしようとするとエラーが発生します。 GeoPtデータをJSONオブジェクトに変換しようとしましたが、動作しませんでした。以下はGoogle App Engine - JSON GeoPtデータのシリアライズ
class Post(ndb.Model):
content = ndb.StringProperty(required = True)
user = ndb.StringProperty()
coords = ndb.GeoPtProperty()
def render(self):
self._render_text = self.content.replace('\n', '<br>')
params = dict(p = self)
return render_str("post.html", **params)
def as_dict(self):
d = {'content': self.content,
'user': self.user,
'coords': self.coords}
return d
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_json(self, d):
json_txt = json.dumps(d)
self.response.headers['Content-Type'] = 'application/json; charset=UTF-8'
self.write(json_txt)
class MainPage(Handler):
def get(self):
posts = Post.query(ancestor=post_key()).order(-Post.created)
if self.format == 'json':
self.response.headers['Access-Control-Allow-Origin'] = '*'
return self.render_json([p.as_dict() for p in posts])
私は適切にJSONでデータを受け取ることができますどのようにトレースバック
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~website/1.396738651304801066/main.py", line 121, in get
return self.render_json([p.as_dict() for p in posts])
File "/base/data/home/apps/s~website/1.396738651304801066/main.py", line 45, in render_json
json_txt = json.dumps(d)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datastore_types.GeoPt(37.636003000000002, 127.216528) is not JSON serializable
のですか?