配置文件用什么格式? JSON 不适合手写,YAML 缩进容易出错。TOML 看似完美,但大多数工具生成的 TOML 嵌套层级太深:
# 默认生成的
[server]
[server.database]
[server.database.connection]
host = "localhost"
port = 5432
人类更喜欢扁平的:
# 实际想要的
[server]
database.connection = {
host = "localhost",
port = 5432,
}
json-pretty-toml - 将 JSON 转换为扁平化 TOML ,遵循三个原则:
[section])section.key = value 或 section."key.name" = {...}){ ... }),使用TOML 1.1的内连风格$ echo '{"gateway":{"port":18789,"auth":{"mode":"token"}}}' | json-pretty-toml
[gateway]
port = 18789
auth.mode = "token"
支持多行内联表、自动引号转义、数组格式化等。
npm install -g json-pretty-toml
或直接用 npx:
npx json-pretty-toml < config.json > config.toml
json-pretty-toml适合需要阅读/偶尔编辑配置文件的场合
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.