Skip to content

Instantly share code, notes, and snippets.

@aggieben
Created January 7, 2010 05:42
Show Gist options
  • Select an option

  • Save aggieben/271024 to your computer and use it in GitHub Desktop.

Select an option

Save aggieben/271024 to your computer and use it in GitHub Desktop.
a little json encoder for use with GAE models
from django.utils import simplejson as json
from google.appengine.ext import db
class GaeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, db.Model):
output = {}
for key, prop in obj.properties().iteritems():
if isinstance(prop, db.ListProperty) and prop.item_type == db.Key:
output[key] = [ db.get(k) for k in getattr(obj, key) ]
else:
output[key] = getattr(obj, key)
return output
elif isinstance(obj, db.Query):
return [ self.default(k) for k in obj ]
return super(self, GaeEncoder).default(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment