Created
January 7, 2010 05:42
-
-
Save aggieben/271024 to your computer and use it in GitHub Desktop.
a little json encoder for use with GAE models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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