@dreamlake/json-layer

中文文档
0.2.0发布于 2026/10/10MITTypeScriptES Module零依赖近 30 天下载 3236

分层的JSON配置:由静态覆盖和运算器组成的JSON行,按顺序解决,每个值的源行.

中文使用文档
文档目录17 节
  1. 覆盖
  2. 运算符
  3. 插入
  4. 预选
  5. 工会
  6. 删除
  7. 删除
  8. 替换
  9. 合并
  10. 全值
  11. 路径
  12. 评论
  13. 没有保留
  14. 错误
  15. 国 际
  16. 参考文献
  17. 许可证

@dreamlake/json-layer (英语)

JSON上层改变 每行一变.

npm install @dreamlake/json-layer
import { resolve, explain } from '@dreamlake/json-layer';

const { value, touches } = resolve(base, [
  { id: 'provider', text: providerLayer },
  { id: 'team', text: teamLayer },
]);
explain(touches, 'network.subnets.b'); // { layer: 'team', line: 3, path: [...] }

下面的每一个例子都是一个基础,一层,以及结果. 他们作为测试运行.

覆盖

对象行合并对象并替换其他所有对象.

{"type": "t3.large", "tags": {"team": "a", "cost": "x"}, "groups": ["sg-1"], "disk": 100}
{"type": "g5.xlarge", "tags": {"team": "b"}, "groups": ["sg-2"], "disk": null}
{"type": "g5.xlarge", "tags": {"team": "b", "cost": "x"}, "groups": ["sg-2"], "disk": null}

线条依序适用.

{}
{"a": 1}
{"a": 2, "b": 3}
{"a": 2, "b": 3}

运算符

[operator, path, ...values]

插入

{"setup": ["apt update"]}
["insert", "setup", "pip install -e .", "make"]
["insert", "env.paths", "/opt/bin"]
{"setup": ["apt update", "pip install -e .", "make"], "env": {"paths": ["/opt/bin"]}}

预选

{"setup": ["make"]}
["prepend", "setup", "apt update"]
{"setup": ["apt update", "make"]}

工会

{"groups": ["sg-1", "sg-2"]}
["union", "groups", "sg-2", "sg-3"]
{"groups": ["sg-1", "sg-2", "sg-3"]}

删除

{"groups": ["sg-1", "sg-2", "sg-1"], "mounts": [{"src": "/a", "dst": "/b"}, {"src": "/c", "dst": "/d"}]}
["remove", "groups", "sg-1"]
["remove", "mounts", {"dst": "/b", "src": "/a"}]
{"groups": ["sg-2"], "mounts": [{"src": "/c", "dst": "/d"}]}

删除

{"tags": {"team": "a", "scratch": "yes"}}
["delete", "tags.scratch"]
["delete", "tags.missing"]
{"tags": {"team": "a"}}

替换

{"tags": {"team": "a", "cost": "x"}}
["replace", "tags", {"team": "b"}]
{"tags": {"team": "b"}}

合并

{"network": {"vpc": "vpc-1", "subnets": {"a": "subnet-a", "b": "subnet-b"}}}
["merge", "network.subnets", {"b": "subnet-b2", "c": {"id": "subnet-c"}}]
{"network": {"vpc": "vpc-1", "subnets": {"a": "subnet-a", "b": "subnet-b2", "c": {"id": "subnet-c"}}}}

全值

{"a": 1}
["replace", "", {"b": 2}]
["merge", "", {"c": 3}]
{"b": 2, "c": 3}

路径

{}
["replace", "a.b.c", 1]
["replace", ["x.y", "z"], 2]
{"p.q": 3}
{"a": {"b": {"c": 1}}, "x.y": {"z": 2}, "p.q": 3}

评论

{"groups": []}
// whole lines only

  // indented is fine
["insert", "groups", "sg-1"]
{"url": "https://example.com//kept"}
{"groups": ["sg-1"], "url": "https://example.com//kept"}

没有保留

{}
{"$insert": ["x"], "note": "[\"delete\", \"a\"]"}
{"$insert": ["x"], "note": "[\"delete\", \"a\"]"}

错误

{"a": 1}
["insrt", "a", 1]
L:2: unknown operator "insrt"
{"a": 1} // no trailing comments
L:1: not JSON
["merge", "a", {
  "b": 1}]
L:1: not JSON
{"tags": "none"}
["insert", "tags", "x"]
L:1: insert needs an array at tags

国 际

json-layer resolve base.json provider.jsonl team.jsonl
json-layer resolve base.json provider.jsonl team.jsonl --explain network.subnets.b
# network.subnets.b	team.jsonl:3 (network.subnets.b)
json-layer check team.jsonl

参考文献

线条效果
{...}合并对象, 替换其余对象
["merge", p, {...}]同样,在 p
["replace", p, v]设定 p 改为 v
["delete", p]删除 p 如果有的话
["insert", p, v...]附加到数组
["prepend", p, v...]添加到阵列前方
["union", p, v...]未显示的附加值
["remove", p, v...]删除等值
// ...注释, 全行
  • p数字 : "a.b.c",或 ["a.b", "c"] 当一个键有一个点; "" 是整个值.
  • 失踪父母的产生,除非 delete 财务报告和审定财务报表 remove。 。 。 .
  • 矩阵按值匹配,从不索引.
  • 每行一纸.
  • 注释行会制作一层非收缩的JSON行.

许可证

麻省理工学院

版本与发布记录

@dreamlake/json-layer 共发布 3 个版本,首次发布于 2026/10/10;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.2.0latest2026/10/101 个月前
0.1.02026/10/101 个月前
0.0.0-stage2026/10/101 个月前

关于 @dreamlake/json-layer 的常见问题

@dreamlake/json-layer 是什么?

分层的JSON配置:由静态覆盖和运算器组成的JSON行,按顺序解决,每个值的源行.

如何安装 @dreamlake/json-layer?

在项目目录运行 npm install @dreamlake/json-layer,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 json-layer,可以用 npx json-layer 直接运行。

@dreamlake/json-layer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dreamlake/json-layer 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dreamlake/json-layer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dreamlake/json-layer 可以商用吗?

@dreamlake/json-layer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dreamlake/json-layer 的下载量有多少?

根据 npm 官方下载统计,@dreamlake/json-layer 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,236 次,日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dreamlake/json-layer 还在维护吗?

最新版本 0.2.0 发布于 2026/10/10(1 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/10/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。