Skip to content

Commit 6913035

Browse files
committed
tools: 迁移脚本支持尾逗号
1 parent f9db19b commit 6913035

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

tools/migrate_pipeline_v5.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,50 @@ def remove_jsonc_comments_for_parsing(text: str) -> str:
9898
return "".join(result)
9999

100100

101+
def remove_trailing_commas(text: str) -> str:
102+
"""移除 JSON 中的尾逗号(在 ] 或 } 之前的逗号)"""
103+
result = []
104+
i = 0
105+
in_string = False
106+
107+
while i < len(text):
108+
if in_string:
109+
if text[i] == "\\" and i + 1 < len(text):
110+
result.append(text[i : i + 2])
111+
i += 2
112+
continue
113+
elif text[i] == '"':
114+
in_string = False
115+
result.append(text[i])
116+
i += 1
117+
continue
118+
119+
if text[i] == '"':
120+
in_string = True
121+
result.append(text[i])
122+
i += 1
123+
continue
124+
125+
if text[i] == ",":
126+
j = i + 1
127+
while j < len(text) and text[j] in " \t\n\r":
128+
j += 1
129+
if j < len(text) and text[j] in "]}":
130+
i += 1
131+
continue
132+
133+
result.append(text[i])
134+
i += 1
135+
136+
return "".join(result)
137+
138+
101139
def parse_jsonc(text: str) -> Any:
102140
"""解析 JSONC 文件内容,返回 OrderedDict 以保持字段顺序"""
103141
import json
104142

105143
clean_text = remove_jsonc_comments_for_parsing(text)
144+
clean_text = remove_trailing_commas(clean_text)
106145
return json.loads(clean_text, object_pairs_hook=OrderedDict)
107146

108147

0 commit comments

Comments
 (0)