JSON5 是对 JSON 文件格式的扩展,旨在简化手工编写和维护(如配置文件),一般用于配置文件中,不用于程序之间的通信。
介绍
在 JSON 中不支持的下列 ECMAScript 5.1 功能已扩展至 JSON5:
- 对象 Objects
- 对象键可以是 ECMAScript 5.1 IdentifierName
- 对象的尾部可以有一个逗号
- 数组 Arrays:可以使用一个逗号
- 字符串 Strings:
- 字符串可使用
单引号('')
- 字符串可以通过转义新行字符来跨多行
- 字符串可包含转义字符
- 数字 Numbers
- 数字可以是十六进制
- 数字可以有小数点前或小数点后
- 数字可以是 IEEE 754 正无穷大、负无穷大和 NaN
- 数字可以以明确的加号开头
- 注释 Comments:允许单行和多行注释
- 留白 White Space:允许使用额外的空白字符
lib 库
# node
npm install json5
示例
{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}