2008年4月19日土曜日

Google App Engine /GqlQuery object and JSON


how to convert GqlQuery object to JSON format



http://groups.google.com/group/google-appengine/browse_thread/thread/634e7b9a2e584dd3?hl=en

How to use simplejson with GqlQuary object for example how to convert
marker_list to JSON for format
marker_list = db.GqlQuery('SELECT * FROM Marker')

I think you should dump queryset or row to python data, then convert
them to json format, here is my testing code:

def dumprow(r):
import datetime
a = {}
for k in r.fields().keys():
v = getattr(r, k)
a['id'] = r.key().id()
a['key'] = str(r.key())
if isinstance(v, str):
a[k] = str(v)
elif isinstance(v, unicode):
a[k] = unicode(v)
elif isinstance(v, datetime.datetime):
a[k] = v.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(v, datetime.date):
a[k] = v.strftime('%Y-%m-%d')
elif isinstance(v, datetime.time):
a[k] = v.strftime('%H:%M:%S')
elif isinstance(v, (int, float, list)):
a[k] = v
else:
a[k] = str(v)

return a

def dumpquery(query):
s = []
for r in query:
s.append(dumprow(r))
return s

Migration Python2.7への移行

1. 以前のプログラムのソースをダウンロードして、 app.xml を変更して deployする 503 python -V // まず Mac にインストールされている python のバージョンを確認 504 pwd 505 curl https://sdk.cl...