admin管理员组文章数量:1307006
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 message = {
"senderID": [ 0x01 ],
"receiverID": [ 0xFF ],
"commandCode": [ 0x00, 0x05 ],
"payload": [ 0xFF ]
}
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 message = {
"senderID": [ 0x01 ],
"receiverID": [ 0xFF ],
"commandCode": [ 0x00, 0x05 ],
"payload": [ 0xFF ]
}
Share
Improve this question
asked Oct 5, 2018 at 18:51
Andrei OnigaAndrei Oniga
8,53917 gold badges54 silver badges91 bronze badges
2 Answers
Reset to default 46JSON does not support hexadecimal numbers but they are supported in JSON5. json5.org
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, 2nd edition, December 2017:
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.
The spec also explains why this is restriction is beneficial to both producer and consumer:
JSON is agnostic about the semantics of numbers. In any programming language, there can be a variety of number types of various capacities and complements, fixed or floating, binary or decimal. That can make interchange between different programming languages difficult. JSON instead offers only the representation of numbers that humans use: a sequence of digits. All programming languages know how to make sense of digit sequences even if they disagree on internal representations. That is enough to allow interchange.
本文标签: javascriptCan hex format be used with JSON files If sohowStack Overflow
版权声明:本文标题:javascript - Can hex format be used with JSON files? If so, how? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737209832a1967379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论