@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行.
许可证
麻省理工学院