zoukankan      html  css  js  c++  java
  • 浅谈JSON5

    在开发Panda Api的最初版本,文档是用json来写的,json语法不支持注释,不支持字符串换行,所有的key都必须双引号,末尾不能有多余的逗号...等等,一大堆极其严格的要求和不支持的功能。由此诞生Json5

    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值可以使用单引号来包裹 Arrays may have a single trailing comma.

    {
        $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
    World
    !",
    }

    数字 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://link.zhihu.com/?target=https%3A//github.com/json5/json5

    总结:JSON5 标准,它有以下特性:
    
    对象
    对象的 key 可以跟 JavaScript 中对象 key 完全一致
    末尾可以有一个逗号
    
    
    数组
    末尾可以有一个逗号
    
    
    字符串
    字符串可以用单引号
    字符串可以用反引号
    字符串可以用转义字符
    
    数字
    数字可以是 16 进制
    数字可以用点开头或结尾
    数字可以表示正无穷、负无穷和NaN.
    数字可以用加号开头
    
    评论
    支持单行和多行注释
    
    空格
    允许多余的空格
  • 相关阅读:
    MYSQL之查询篇
    MYSQL之概念基础篇
    订餐系统之组合捜索
    订餐系统之微信点餐
    订餐系统之定时器Timer不定时
    订餐系统之地图订餐
    结构型模式:装饰模式
    结构型模式:组合模式
    结构型模式:桥接模式
    结构型模式:适配器模式
  • 原文地址:https://www.cnblogs.com/cy0628/p/15179838.html
Copyright © 2011-2022 走看看