This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathjson.py
69 lines (53 loc) · 1.89 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from abc import ABCMeta, abstractmethod as abstract_method
from json.encoder import JSONEncoder as BaseJsonEncoder
from typing import Iterable, Mapping
class JsonSerializable(object, metaclass=ABCMeta):
"""
Interface for classes that can be safely converted to JSON.
"""
@abstract_method
def as_json_compatible(self):
"""
Returns a JSON-compatible representation of the object.
References:
- :py:class:`iota.json.JsonEncoder`.
"""
raise NotImplementedError(
'Not implemented in {cls}.'.format(cls=type(self).__name__),
)
def _repr_pretty_(self, p, cycle):
"""
Makes JSON-serializable objects play nice with IPython's default
pretty-printer.
Sadly, :py:func:`pprint.pprint` does not have a similar
mechanism.
References:
- http://ipython.readthedocs.io/en/stable/api/generated/IPython.lib.pretty.html
- :py:meth:`IPython.lib.pretty.RepresentationPrinter.pretty`
- :py:func:`pprint._safe_repr`
"""
class_name = type(self).__name__
if cycle:
p.text('{cls}(...)'.format(
cls=class_name,
))
else:
with p.group(
len(class_name) + 1,
'{cls}('.format(cls=class_name),
')',
):
prepared = self.as_json_compatible()
if isinstance(prepared, Mapping):
p.text('**')
elif isinstance(prepared, Iterable):
p.text('*')
p.pretty(prepared)
class JsonEncoder(BaseJsonEncoder):
"""
JSON encoder with support for :py:class:`JsonSerializable`.
"""
def default(self, o):
if isinstance(o, JsonSerializable):
return o.as_json_compatible()
return super(JsonEncoder, self).default(o)