-
Notifications
You must be signed in to change notification settings - Fork 8
/
jsonparse.py
110 lines (86 loc) · 2.54 KB
/
jsonparse.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from functools import reduce
import re
import pprint
def array_parser(data):
if data[0] != "[":
return None
parse_list = []
data = data[1:].strip()
while len(data):
res = value_parser(data)
if res is None:
return None
parse_list.append(res[0])
data = res[1].strip()
if data[0] == "]":
return [parse_list, data[1:].strip()]
res = comma_parser(data)
if res is None:
return None
data = res[1].strip()
def boolean_parser(data):
if data[0:4] == "true":
return [True, data[4:].strip()]
elif data[0:5] == "false":
return [False, data[5:].strip()]
def colon_parser(data):
if data[0] == ":":
return [data[0], data[1:].lstrip()]
def comma_parser(data):
if data and data[0] == ",":
return [data[0], data[1:].strip()]
def null_parser(data):
if data[0:4] == "null":
return [None, data[4:].strip()]
def number_parser(data):
regex_find = re.findall("^(-?(?:[0-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)",
data)
if not regex_find:
return None
pos = len(regex_find[0])
try:
return [int(regex_find[0]), data[pos:].strip()]
except ValueError:
return [float(regex_find[0]), data[pos:].strip()]
def object_parser(data):
if data[0] != "{":
return None
parse_dict = {}
data = data[1:].strip()
while data[0] != "}":
res = string_parser(data)
if res is None:
return None
id = res[0]
res = colon_parser(res[1].strip())
if res is None:
return None
res = value_parser(res[1].strip())
if res is None:
return None
parse_dict[id] = res[0]
data = res[1].lstrip()
res = comma_parser(data)
if res:
data = res[1].strip()
return [parse_dict, data[1:]]
def string_parser(data):
if data[0] == '"':
data = data[1:]
pos = data.find('"')
return [data[:pos], data[pos + 1:].strip()]
def all_parsers(*args):
return lambda data: (reduce(lambda f, g: f if f(data) else g, args)(data))
value_parser = all_parsers(null_parser, number_parser, boolean_parser,
string_parser, object_parser, array_parser)
def main():
file_name = "ex3.json"
with open(file_name, "r") as f:
data = f.read()
res = value_parser(data.strip())
try:
pprint.pprint(res[0])
except TypeError:
print(None)
if __name__ == "__main__":
main()