Created
November 4, 2011 14:09
-
-
Save kwylez/1339398 to your computer and use it in GitHub Desktop.
Convert Python Class Instance to JSON
This file contains 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
# 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