Skip to content

Instantly share code, notes, and snippets.

@Joseph507
Created January 12, 2016 17:58
Show Gist options
  • Save Joseph507/0d747700ad67f5b9a908 to your computer and use it in GitHub Desktop.
Save Joseph507/0d747700ad67f5b9a908 to your computer and use it in GitHub Desktop.
SQL Alchemy JSON Encoder
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