-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonModule.py
192 lines (172 loc) · 6.15 KB
/
JsonModule.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from enum import Enum
class JsonSerializer:
@classmethod
def dumps(cls, j):
result = ''
if type(j) == dict:
result += '{'
for i in j:
result += cls.dumps(i) + ": " + cls.dumps(j[i]) + ", "
if result.endswith(", "):
result = result[0:-2]
result += '}'
elif type(j) == list:
result += '['
for i in j:
result += cls.dumps(i) + ", "
if result.endswith(", "):
result = result[0:-2]
result += ']'
elif type(j) == str:
result = "\"" + j + "\""
elif type(j) == int or type(j) == float:
result = str(j)
elif j == True:
result = "true"
elif j == False:
result = "false"
elif j == None:
result = "null"
else:
raise Exception(j)
return result
class TOKEN(Enum):
INVALID = 0 # INVALID
CURLY_OPEN = 1 # {
CURLY_CLOSE = 2 # }
SQUARED_OPEN = 3 # [
SQUARED_CLOSE = 4 # ]
COLON = 5 # :
COMMA = 6 # ,
STRING = 7 # "..."
NUMBER = 8 # +-0.123456789
TRUE = 9 # true
FALSE = 10 # false
NULL = 11 # null
class TokenReader:
def __init__(self, token_list):
self._id = 0
self._token_list = token_list
def next_token(self):
token = self._token_list[self._id + 1]
return token
def read_token(self):
token = self._token_list[self._id]
self._id += 1
return token
class JsonDeserializer:
@classmethod
def str2tokens(cls, string):
curr = ""
token_list = []
reading_str = False
for c in string:
if reading_str:
if c == '"':
reading_str = False
token_list.append((TOKEN.STRING, curr))
curr = ""
else:
curr += c
elif c in ' \t\r\n{}[]:,':
if curr == "":
pass
elif curr == 'true':
token_list.append((TOKEN.TRUE, True))
curr = ""
elif curr == 'false':
token_list.append((TOKEN.FALSE, False))
curr = ""
elif curr == 'null':
token_list.append((TOKEN.NULL, None))
curr = ""
else:
try:
if '.' in curr:
num = float(curr)
else:
num = int(curr)
except Exception as e:
for token in token_list:
print(token)
raise Exception("failed to parse token {0}".format(curr))
token_list.append((TOKEN.NUMBER, num))
curr = ""
if c == '{':
token_list.append((TOKEN.CURLY_OPEN, c))
elif c == '}':
token_list.append((TOKEN.CURLY_CLOSE, c))
elif c == '[':
token_list.append((TOKEN.SQUARED_OPEN, c))
elif c == ']':
token_list.append((TOKEN.SQUARED_CLOSE, c))
elif c == ':':
token_list.append((TOKEN.COLON, c))
elif c == ',':
token_list.append((TOKEN.COMMA, c))
elif c == '"':
reading_str = True
elif c in ' \t\r\n':
pass
else:
curr += c
curr = curr.strip()
if curr:
token_list.append((TOKEN.NONE, curr))
return token_list
@classmethod
def tokenReader2json(cls, token_reader):
result = None
curr = token_reader.read_token()
if curr[0] == TOKEN.CURLY_OPEN:
result = result or {}
next_token = token_reader.next_token()
if next_token[0] == TOKEN.CURLY_CLOSE:
token_reader.read_token()
return result
while True:
curr = token_reader.read_token()
assert curr[0] == TOKEN.STRING, curr
key = curr[1]
curr = token_reader.read_token()
assert curr[0] == TOKEN.COLON, curr
value = cls.tokenReader2json(token_reader)
result[key] = value
curr = token_reader.read_token()
assert curr[0] in [TOKEN.CURLY_CLOSE, TOKEN.COMMA], curr
if curr[0] == TOKEN.CURLY_CLOSE:
return result
elif curr[0] == TOKEN.SQUARED_OPEN:
result = result or []
next_token = token_reader.next_token()
if next_token[0] == TOKEN.SQUARED_CLOSE:
token_reader.read_token()
return result
while True:
value = cls.tokenReader2json(token_reader)
result.append(value)
curr = token_reader.read_token()
assert curr[0] in [TOKEN.SQUARED_CLOSE, TOKEN.COMMA], curr
if curr[0] == TOKEN.SQUARED_CLOSE:
return result
elif curr[0] in [TOKEN.STRING, TOKEN.NUMBER, TOKEN.TRUE, TOKEN.FALSE, TOKEN.NULL]:
return curr[1]
elif curr[0] != TOKEN.INVALID:
return None
else:
raise
@classmethod
def tokenList2json(cls, token_list):
reader = TokenReader(token_list)
return cls.tokenReader2json(reader)
@classmethod
def loads(cls, json_str):
token_list = cls.str2tokens(json_str)
return cls.tokenList2json(token_list)
class Json:
@classmethod
def dumps(cls, obj):
return JsonSerializer.dumps(obj)
@classmethod
def loads(cls, json_str):
return JsonDeserializer.loads(json_str)