Skip to content

Instantly share code, notes, and snippets.

@kwylez
Created November 4, 2011 14:09
Show Gist options
  • Save kwylez/1339398 to your computer and use it in GitHub Desktop.
Save kwylez/1339398 to your computer and use it in GitHub Desktop.
Convert Python Class Instance to JSON
# http://stackoverflow.com/questions/1531501/json-serialization-of-google-app-engine-models
class jsonEncoder(simplejson.JSONEncoder):
def default(self, obj):
isa = lambda x: isinstance(obj, x) # isa(<type>)==True if obj is of type <type>
return obj.isoformat() if isa(datetime.datetime) else \
db.to_dict(obj) if isa(db.Model) else \
obj.email() if isa(users.User) else \
simplejson.JSONEncoder.default(self, obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment