forked from katjabercic/Lean-HoG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonEncoder.py
36 lines (33 loc) · 1.31 KB
/
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
32
33
34
35
36
import json
from graph import Edge, Graph
from treeSet import Tree
from treeMap import Map, TreeMap
from components_certificate import ComponentsCertificate
def graph_to_json(graph : Graph) -> dict:
nbhMap = TreeMap.from_dict({v : Tree.from_set(nbh)
for (v, nbh) in graph.neighborhood_map().items()})
degMap = TreeMap.from_dict(graph.degree_map())
return {
"graph" : graph,
"edgeSize" : graph.edge_size(),
"neighborhoodMap" : Map(emptyDomain=graph.is_empty(), defaultValue=Tree(), tree=nbhMap),
"degreeMap" : Map(emptyDomain=graph.is_empty(), defaultValue=0, tree=degMap),
"componentsCertificate" : ComponentsCertificate(graph)
}
class GraphEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Edge):
return obj.to_json()
elif isinstance(obj, Graph):
return obj.to_json()
elif isinstance(obj, Tree):
return obj.to_json()
elif isinstance(obj, TreeMap):
return obj.to_json()
elif isinstance(obj, Map):
return obj.to_json()
elif isinstance(obj, ComponentsCertificate):
return obj.to_json()
else:
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)