Skip to content

Instantly share code, notes, and snippets.

@lsbardel
Created November 26, 2010 07:53
Show Gist options
  • Save lsbardel/716393 to your computer and use it in GitHub Desktop.
Save lsbardel/716393 to your computer and use it in GitHub Desktop.
A Json encoder/decoder for Python 2.6 or above
import time
from datetime import date, datetime
from decimal import Decimal
import json
def totimestamp(dte):
return time.mktime(dte.timetuple())
def totimestamp2(dte):
return totimestamp(dte) + 0.000001*dte.microsecond
def todatetime(tstamp):
return datetime.fromtimestamp(tstamp)
class JSONDateDecimalEncoder(json.JSONEncoder):
"""
Provide custom serializers for JSON-RPC.
"""
def default(self, obj):
if isinstance(obj,datetime):
return {'__datetime__':totimestamp2(obj)}
elif isinstance(obj, date):
return {'__date__':totimestamp(obj)}
elif isinstance(obj, Decimal):
return {'__decimal__':str(obj)}
else:
raise ValueError("%r is not JSON serializable" % (obj,))
def date_decimal_hook(dct):
if '__datetime__' in dct:
return todatetime(dct['__datetime__'])
elif '__date__' in dct:
return todatetime(dct['__date__']).date()
elif '__decimal__' in dct:
return Decimal(dct['__decimal__'])
else:
return dct
DefaultJSONEncoder = JSONDateDecimalEncoder
DefaultJSONHook = date_decimal_hook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment