GoForum › 🌐 V2EX
我做了一个把 JSON 转换成人类更容易阅读/编辑风格的 TOML
Cbdy ·
2026-03-22 21:29 ·
0 次点赞 · 1 条回复
痛点
配置文件用什么格式? 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 ,遵循三个原则:
- 只有一层 Table(
[section]) - Key 最多两层(
section.key = value或section."key.name" = {...}) - 其余全部内联(
{ ... })
示例
$ 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
链接
- GitHub: https://github.com/nano-properties/json-pretty-toml
- npm:
json-pretty-toml
适合需要阅读/偶尔编辑配置文件的场合
1 条回复
添加回复
你还需要 登录
后发表回复
让我想起了 ini 这种配置文件。尤其是 win95 的 system.ini 和 win.ini
历史就是个大轮回啊。