我做了一个把 JSON 转换成人类更容易阅读/编辑风格的 TOML

8 小时 11 分钟前
 Cbdy

痛点

配置文件用什么格式? 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 ,遵循三个原则:

  1. 只有一层 Table[section]
  2. Key 最多两层section.key = valuesection."key.name" = {...}
  3. 其余全部内联{ ... }),使用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

链接

适合需要阅读/偶尔编辑配置文件的场合

84 次点击
所在节点    分享创造
1 条回复
Dispatcher
7 小时 49 分钟前
让我想起了 ini 这种配置文件。尤其是 win95 的 system.ini 和 win.ini

历史就是个大轮回啊。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1200200

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX