Can hex format be used with JSON files? If so, how?

后端 未结 2 398
温柔的废话
温柔的废话 2021-01-08 01:13

The following object is a valid one in plain Javascript. However, if the same is added to a JSON file, the file does not pass validation. Why is that?

var me         


        
2条回答
  •  生来不讨喜
    2021-01-08 01:30

    The JSON spec supports numbers as values but explicitly does not support octal or hexidecimal. This is in part to increase interchange between languages. You could just as easily represent 0xFF as a string, "0xFF" and parse that out when using it.


    From json.org:

    A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

    According to the ECMA-404 Final draft:

    A number is a sequence of decimal digits with no superfluous leading zero. It may have a preceding minus sign (U+002D). It may have a fractional part prefixed by a decimal point (U+002E). It may have an exponent, prefixed by e (U+0065) or E (U+0045) and optionally + (U+002B) or – (U+002D). The digits are the code points U+0030 through U+0039.

提交回复
热议问题