forked from getsentry/raven-python
/
json.py
30 lines (24 loc) · 832 Bytes
/
json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
raven.utils.json
~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import datetime
import simplejson
import uuid
class BetterJSONEncoder(simplejson.JSONEncoder):
def default(self, obj):
if isinstance(obj, uuid.UUID):
return obj.hex
elif isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
elif isinstance(obj, (set, frozenset)):
return list(obj)
return super(BetterJSONEncoder, self).default(obj)
def better_decoder(data):
return data
def dumps(value, **kwargs):
return simplejson.dumps(value, cls=BetterJSONEncoder, **kwargs)
def loads(value, **kwargs):
return simplejson.loads(value, object_hook=better_decoder)