-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (38 loc) · 961 Bytes
/
main.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
# Read JSON as string
s = """
{
"k1": "value \"hey\" you",
"k2": -10.57,
"k3": {},
"k4": {"k1": null},
"k5": [1, "hello"]
}
"""
def isString(prev_char, char):
inStr = True
isQuotation = char == '"'
escapeChar = prev_char == "\\"
if isQuotation and not escapeChar:
return inStr
else:
return not inStr
def tokenize(json_str: str) -> list:
i = 0
tokenized = []
while i < len(json_str):
char = json_str[i]
if char == "{":
tokenized.append("OPEN_DICT"+str(i))
elif char == "}":
tokenized.append("CLOSE_DICT"+str(i))
elif char == ":":
tokenized.append("COLON")
elif char == ",":
tokenized.append("COMMA")
elif char == "[":
tokenized.append("OPEN_ARR")
elif char == "]":
tokenized.append("CLOSE_ARR")
i += 1
return tokenized
print(tokenize(s))