Skip to content

Instantly share code, notes, and snippets.

@JeffreyMFarley
Created August 17, 2023 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyMFarley/23f23a263e88f198c0d514935961760f to your computer and use it in GitHub Desktop.
Save JeffreyMFarley/23f23a263e88f198c0d514935961760f to your computer and use it in GitHub Desktop.
from datetime import date, datetime
from decimal import Decimal
def json_serial(obj):
"""Helps json.dumps render datetime objects"""
if isinstance(obj, date):
return obj.isoformat()[:10]
if isinstance(obj, datetime):
return obj.isoformat()[:10]
if isinstance(obj, Decimal):
return f'{obj.normalize():f}'
raise TypeError(f"Type {type(obj)} not serializable")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment