-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONDecoder_2.py
56 lines (50 loc) · 1.56 KB
/
JSONDecoder_2.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
# Creating a custom JSONDecoder using the point schema.
import json
import re
from pprint import pprint
from decimal import Decimal
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def __repr__(self):
return f'Point(x={self.x}, y={self.y})'
class CustomDecoder(json.JSONDecoder):
base_decoder = json.JSONDecoder(parse_float = Decimal)
def decode(self, arg):
obj = self.base_decoder.decode(arg)
pattern = r'"_type"\s*:\s*"point"'
if re.search(pattern, arg):
obj = self.make_pts(obj)
return obj
def make_pts(self, obj):
if isinstance(obj, dict):
if obj.get('_type', None) == 'point':
obj = Point(obj['x'], obj['y'])
else:
for key, value in obj.items():
obj[key] = self.make_pts(value)
elif isinstance(obj, list):
for index, item in enumerate(obj):
obj[index] = self.make_pts(item)
return obj
j = '''
{
"a": 100,
"b": 0.5,
"rectangle": {
"corners": {
"b_left": {"_type": "point", "x": -1, "y": -1},
"b_right": {"_type": "point", "x": 1, "y": -1},
"t_left": {"_type": "point", "x": -1, "y": 1},
"t_right": {"_type": "point", "x": 1, "y": 1}
},
"rotate": {"_type": "point", "x": 0, "y": 0},
"interior_pts": [
{"_type": "point", "x":0, "y":0},
{"_type": "point", "x":0.5, "y":0.5}
]
}
}
'''
pprint(json.loads(j, cls=CustomDecoder))