-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_datetimes.py
More file actions
38 lines (24 loc) · 760 Bytes
/
json_datetimes.py
File metadata and controls
38 lines (24 loc) · 760 Bytes
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
31
32
33
34
35
36
37
38
#!/usr/bin/env python
'''
json_datetimes.py - Allow json to handle datetime serialisation transparently
description
Author: Eric Saunders
September 2012
'''
import json
from datetime import datetime
dt_handler1 = lambda obj: obj.isoformat() if isinstance(obj, datetime) else None
class hello(object):
def __init__(self, **kwargs):
pass
def encode(self, obj):
print obj
return "hello!"
def dt_handler2(obj):
print "Entered dt_handler2"
if hasattr(obj, 'isoformat'):
return obj.isoformat()
data_structure = { 'a' : 4, 'b' : 'herring', 'c': datetime.now(), 'd':json }
#print json.dumps(data_structure, default=dt_handler1)
print json.dumps(data_structure, cls=hello)
#print json.dumps(data_structure)