(text: string)
| 3 | |
| 4 | // 将 python dict 转成 JSON |
| 5 | export async function pythonDictToJSON(text: string) { |
| 6 | const options = (await getConfig()).parseOptions; |
| 7 | text = text |
| 8 | .replace(/,\s*\}(\W*?)/gm, "}$1") |
| 9 | .replace(/,\s*\](\W*?)/gm, "]$1") |
| 10 | .replace(/(\W*?)'/gm, '$1"') |
| 11 | .replace(/'(\W*?)/gm, '"$1') |
| 12 | .replace(/\bTrue\b/gm, "true") |
| 13 | .replace(/\bFalse\b/gm, "false") |
| 14 | .replace(/\bNone\b/gm, "null"); |
| 15 | const tree = parseJSON(text, options); |
| 16 | return tree.valid() ? tree.stringify(options) : text; |
| 17 | } |