Json5怎一个爽字了得

JSON(JavaScript Object Notation),一种轻量级的数据交换格式,源于JavaScript,为ECMAScript的一个子集。其易于阅读和编写、易于编码和解析等特性,已被广泛应用于尤其配置及http下的序列化等众多方面。

json语法不支持注释,不支持字符串换行,所有的key都必须双引号,末尾不能有多余的逗号...等等,一大堆极其严格的要求和不支持的功能。

JSON5 和 JSON 的关系就像 ECMAScript 5 和 ECMAScript 3 的关系。JSON5 同样是 ECMAScript 5 的严格的子集。

JSON5是对JSON的扩展,其目的是能够让人们(程序猿)更加容易的阅读和编写。
按照官方介绍,JSON5相对于JSON增加了以下特性(优势) summary-of-features

这完全就是我想要的!安利给大家。

Json5 语法说明

JSON5是对JSON的扩展,让人可以更容易手工编写和维护。

JSON5的特性如下:

对象 Objects

  • key值允许没有双引号 Object keys may be an ECMAScript 5.1 IdentifierName.
{
    code:1,
    msg:"Hello"
}
  • 允许有多余的逗号结尾 Objects may have a single trailing comma.
{
    code:1,
    msg:"Hello",
}
  • key值可以使用单引号来包裹 The key value can be wrapped in single quoted.
{
    $name:"HellO",
    code:1,
    msg:'Hello',
}

数组 Arrays

  • 允许有多余的逗号结尾
[1, 2, 3,]

字符串 Strings

  • 允许使用单引号包裹字符串 Strings may be single quoted.
{
    $name:'HellO',
    code:1,
    msg:'Hello',
}
  • 字符串可以换行,可以多行 Strings may span multiple lines by escaping new line characters
{
    $name:'HellO',
    $desc:"hello
    world!

    haha!
    ",
    code:1,
    msg:'Hello',
}
  • 字符串允许使用转义字符 Strings may include character escapes.
{
    code:1,
    msg:"Hello\nWorld\n!",
}

数字 Numbers

  • 数字可以用十六进制表示 Numbers may be hexadecimal.
{
    code:0xFF
}
  • 允许使用小数点开头或结尾的数字,例如:.0077. Numbers may have a leading or trailing decimal point.
{
    a:.007,
    b:7.
    f:-.32e-3
}
  • 数字可以使用正无穷大、负无穷大、和Nan 来表示, Numbers may be IEEE 754 positive infinity, negative infinity, and NaN.
{
    p:Infinity,
    n:-Infinity,
    b:NaN
}
  • 数字前面可以有一个正号+ Numbers may begin with an explicit plus sign. { a:+10 }

注释 Comments

  • 支持单行注释和多行注释 Single and multi-line comments are allowed.
{
    // code表示结果代码
    code:1
    msg:"返回结果说明"
    /*
        json5的多行注释
        真是好爽啊,
        完全就是为Panda api而设计的
    */
}

允许多余的空白符 White Space

  • 允许在任何在json5相关符号之前和之后都可能出现多余的空白符。 Additional white space characters are allowed. 支持的空白符如下:
Code Points Description
U+0009: Horizontal tab
U+000A: Line feed
U+000B: Vertical tab
U+000C: Form feed
U+000D: Carriage return
U+0020: Space
U+00A0: Non-breaking space
U+2028: Line separator
U+2029: Paragraph separator
U+FEFF: Byte order mark
Unicode: Zs category    Any other character in the Space Separator Unicode category

官方相关说明介绍:

https://github.com/json5/json5​github.com


经典前端面试题每日更新,欢迎参与讨论,地址:https://github.com/daily-interview/fe-interview

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章