-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonencoder.py
31 lines (25 loc) · 882 Bytes
/
jsonencoder.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
import json
from pynanocoin import *
import telemetry_req
from representatives import Representative
class NanoJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytes):
return hexlify(obj)
if isinstance(obj, ip_addr):
return str(obj)
if isinstance(obj, message_header):
return obj.__dict__
if isinstance(obj, network_id):
return obj.id
if isinstance(obj, message_type):
return obj.type
if isinstance(obj, telemetry_req.telemetry_ack):
return obj.__dict__
if isinstance(obj, Peer):
return obj.__dict__
if isinstance(obj, Representative):
return obj.__dict__
return json.JSONEncoder.default(self, obj)
def to_json(peers):
return json.dumps(peers, cls=NanoJSONEncoder, indent=4)