Created
January 12, 2016 17:58
-
-
Save Joseph507/0d747700ad67f5b9a908 to your computer and use it in GitHub Desktop.
SQL Alchemy JSON Encoder
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
import decimal, datetime | |
def alchemyencoder(obj): | |
"""JSON encoder function for SQLAlchemy special classes.""" | |
if isinstance(obj, datetime.date): | |
return obj.isoformat() | |
elif isinstance(obj, str): | |
return obj.strip() | |
elif isinstance(obj, decimal.Decimal): | |
return float(obj) | |
def _example(): | |
res = conn.execute(select([accounts])) | |
# use special handler for dates and decimals | |
return json.dumps([dict(r) for r in res], default=alchemyencoder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment