-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson_utils.py
85 lines (76 loc) · 2.28 KB
/
json_utils.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
def fix_quotes(line):
fixed_line = ""
quote_mode = False
for char in line:
if quote_mode == False:
if char == '"':
quote_mode = True
else:
if char == '"':
quote_mode = False
elif char == ':' or char == '\n':
fixed_line += '"'
quote_mode = False
fixed_line += char
return fixed_line
def fix_backslashes(json_string):
# 不正なバックスラッシュを修正する
fixed_string = json_string.replace("\\", "\\\\")
return fixed_string
def parse_one_entry(line: str, key: str):
line = line.split(key)[1].strip()
string_without_quotes = line.replace('"', '')
entries = string_without_quotes.split(":")
contents = []
#skip DetailedStrategy
#get contents
for entry in entries:
if line in entry:
continue
else:
contents.append(entry)
new_contents = " ".join(contents)
#recreate line
new_line = '"' + key + '": ' + '"' + new_contents + '",'
print(new_line)
return new_line
def parse_plan(org_data: str):
lines = org_data.split("\n")
output_lines = []
start_flag = False
for line in lines:
if start_flag == False:
if "{" in line:
start_flag = True
output_lines.append(fix_quotes(line))
else:
pass
else:
if "DetailedStrategy" in line:
line = parse_one_entry(line, "DetailedStrategy")
output_lines.append(fix_quotes(fix_backslashes(line)))
return "\n".join(output_lines)
def parse_json(org_data: str):
lines = org_data.split("\n")
output_lines = []
start_flag = False
nest_count = 0
for line in lines:
if start_flag == False:
if "{" in line:
start_flag = True
nest_count += 1
output_lines.append(fix_quotes(line))
else:
pass
else:
if "{" in line:
nest_count += 1
elif "}" in line:
nest_count -= 1
output_lines.append(line)
if (nest_count == 0):
break
return "\n".join(output_lines)