-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_funcs.py
51 lines (35 loc) · 1.47 KB
/
json_funcs.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Functions for manipulating JSON.
json_iterable + json_encode allow the serialisation of objects which contain the
`__jsonencode__` method. If called, this should return a serialisable object (simple
python objects).
write_json will use those functions to write out a JSON file.
"""
import json
def json_encode(obj):
"""
Encoding function to use for objects which are not known to the standard JSON encoder.
If the object contains a property '__jsonencode__', it is called to obtain the representation
of the object.
"""
def jsonspecial_datetime(obj): # pylint: disable=unused-variable
return obj.isoformat()
if hasattr(obj, '__jsonencode__'):
return obj.__jsonencode__()
special_name = 'jsonspecial_%s' % (obj.__class__.__name__,)
special_func = locals().get(special_name, None)
if special_func:
return special_func(obj)
raise TypeError("Cannot serialise a '%s' object: %r" % (obj.__class__.__name__, obj))
def json_iterable(obj, pretty=False):
if pretty:
return json.JSONEncoder(default=json_encode,
sort_keys=True,
indent=2,
separators=(',', ': ')).iterencode(obj)
else:
return json.JSONEncoder(default=json_encode).iterencode(obj)
def write_json(filename, obj, pretty=False):
with open(filename, 'w') as fh:
for chunk in json_iterable(obj, pretty=pretty):
fh.write(chunk)