JSON5 解析器
用于维持缩进、间隔和注释的JSON5标注器和解析器.
概览
这个库提供了一个 API ,用于与 JSON 和 杰森5号 文档,同时保留其原始结构和格式. 与使用抽象语法树(AST)和丢弃格式的传统JSON解析器不同,这个库利用了混凝土语法树(CST)来保留每一个细节——注释,缩进和白空格.
理想编辑配置文件(例如, package.json, (中文(简体) ), tsconfig.json)或任何用户生成的JSON5内容,此库确保格式化在整个修改过程中保持不变.
主要特点
- 保留格式化 – 在不丢失注释,缩进或间隔的情况下读取,修改,并写入JSON5文件.
- 样式学习 – 将文档现有的格式样式自动应用于新条目.
- 重新编排格式 – 自定义带有灵活选项的输出格式.
- 类型安全 – Fully 为JSON5文档的工作输入了API.
安装
通过 国家预防机制数字 :
npm install @croct/json5-parser
使用量
库提供了一个简单的API,用于解析,操纵,并串行JSON5文档.
定 法
通常,你不需要直接与Lexer互动. 然而,您可以使用它来表示一个JSON5文档:
import {JsonLexer} from '@croct/json5-parser';
const tokens = JsonLexer.tokenize(
`{
// Comment
"name": "John Doe",
"age": 42,
}`
);
分析
要分析 JSON5 文件 :
import {JsonParser} from '@croct/json5-parser';
const node = JsonParser.parse(
`{
// Comment
"name": "John Doe",
"age": 42,
}`
);
可选地指定要缩小结果的根节点类型 :
import {JsonParser, JsonObjectNode} from '@croct/json5-parser';
const node = JsonParser.parse(
`{
// Comment
"name": "John Doe",
"age": 42,
}`,
JsonObjectNode
);
操纵
保留格式时修改值 :
// Get the value of a property
const name = node.get('name').toJSON();
// Update a property
node.set('age', 43);
console.log(node.toString());
新条目采用文档的现有样式 :
node.set('country', 'USA');
console.log(node.toString());
输出 :
{
// Comment
"name": "John Doe",
"age": 43,
"country": "USA",
}
格式化在块级应用,在同一文档中处理不同的样式:
{
"name": "My Project",
"version": "1.0.0",
"keywords": ["json5", "parser"],
}
添加数组项时保留现有格式 :
node.set('stack', ['react', 'typescript']);
输出 :
{
"name": "My Project",
"version": "1.0.0",
"keywords": ["json5", "parser"],
"stack": ["react", "typescript"],
}
要重置格式并应用新样式:
node.reset();
console.log(node.toString({/* formatting options */}));
输出 :
{
"name": "My Project",
"version": "1.0.0",
"keywords": [
"json5",
"parser"
],
"stack": [
"react",
"typescript"
]
}
参见 格式化 关于现有选项的详情.
要在保存格式化的同时更新文档,请使用 update 方法 :
node.update({
...node.toJSON(),
"version": "2.0.0",
});
这个 update 方法将新内容与现有文档相协调,保留注释、缩进和间隔.
对于单个更新,宁可 set 提高性能的方法:
node.set('version', '2.0.0');
要合并两个文档,同时保留这两个文档的评论和格式,请使用 merge 方法 :
const destinationCode = `{
// Destination pre-foo comment
"foo": "value",
// Destination post-foo comment
"baz": [1, 2, 3]
}
`;
const sourceCode = `{
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */
}
`;
const source = JsonParser.parse(sourceCode, JsonObjectNode);
const destination = JsonParser.parse(destinationCode, JsonObjectNode);
destination.merge(source)
console.log(destination.toString());
输出 :
{
// Destination pre-foo comment
"foo": "value",
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */
}
格式化
当你打电话 toString(options) 您可以控制输出格式化.
如果一个选项被省略,则格式从当前文档中学习并保持其样式。 如果无法推断出风格,
格式重回紧凑的样式,没有额外的空格或缩进.
选项
| 选项 | 类型/价值 | 说明 |
|---|---|---|
indentationLevel | number | 文档的基缩进级. |
indentationCharacter | 'space'|'tab' | 缩进所用的字符 . |
lineEnding | 'lf'|'crlf' | 在文档中使用的新行序列 . |
string.quote | 'single'|'double' | 字符串值的引号样式. |
property.quote | 'single'|'double' | 属性密钥的引号样式 . |
property.unquoted | boolean | 有效时允许未引用的属性密钥 . |
array.indentationSize | number | 数组条目缩进大小 . |
array.trailingIndentation | boolean | 在新线上收缩括号. |
array.leadingIndentation | boolean | 缩进打开新线上的括号. |
array.entryIndentation | boolean | 每个数组条目缩进 . |
array.trailingComma | boolean | 在最后一个条目后添加一个后缀. |
array.commaSpacing | boolean | 在数组中逗号后添加空格 . |
array.colonSpacing | boolean | 在阵列中,在冒号后添加空格(对于内含的对象). |
object.indentationSize | number | 对象属性缩进大小 . |
object.trailingIndentation | boolean | 缩进关闭新线上. |
object.leadingIndentation | boolean | 缩进打开新线上的保险套. |
object.entryIndentation | boolean | 缩进每个对象属性. |
object.trailingComma | boolean | 在最后一个属性后添加后缀逗号 . |
object.commaSpacing | boolean | 在对象中逗号后添加空格. |
object.colonSpacing | boolean | 在对象中以冒号后添加空格. |
快速食谱
这里有一些常见的格式样式,通过结合不同的选项可以实现.
1) 紧凑的单行数组; 漂亮的多行对象
node.toString({
array: {
leadingIndentation: false, // keep arrays inline
trailingIndentation: false,
trailingComma: false,
commaSpacing: true,
},
object: {
entryIndentation: true, // multi-line objects
leadingIndentation: true,
trailingIndentation: true,
indentationSize: 2,
trailingComma: true,
commaSpacing: true,
colonSpacing: true,
},
});
输出样式 :
{
"name": "My Project",
"keywords": ["json5", "parser"], // array kept inline
"deps": {
"typescript": "^5.6.0", // multi-line object
"vite": "^5.4.0",
}
}
2) 各地的标签,单引号,仅在必要时引用属性
node.toString({
indentationCharacter: 'tab',
string: {
quote: 'single'
},
property: {
quote: 'single',
unquoted: true
},
object: {
leadingIndentation: true,
entryIndentation: true,
trailingIndentation: true,
trailingComma: false,
commaSpacing: true,
colonSpacing: true,
},
});
输出样式 :
{
foo: 'bar',
items: [
'a',
'b'
]
}
3) 严格的紧凑样式( 逗号/ 冒号后没有空格)
node.toString();
输出样式 :
{"a":1,"b":[1,2,3]}
捐款
欢迎捐款!
测试
安装依赖性 :
npm install
运行测试 :
npm run test
要检查样式问题的 Lint 代码 :
npm run lint