我正在阅读RFC 4627,但无法确定以下内容是否为有效JSON。考虑以下简约的JSON文本:
["\u005c"]
问题是小写的
c
。根据RFC的文本,允许:
Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. The hexadecimal letters A though F can be upper or lowercase. So, for example, a string containing only a single reverse solidus character may be represented as "\u005C".
(强调我的)
问题在于RFC还包含以下语法:
char = unescaped /
escape (
%x22 / ; " quotation mark U+0022
%x5C / ; \ reverse solidus U+005C
%x2F / ; / solidus U+002F
%x62 / ; b backspace U+0008
%x66 / ; f form feed U+000C
%x6E / ; n line feed U+000A
%x72 / ; r carriage return U+000D
%x74 / ; t tab U+0009
%x75 4HEXDIG ) ; uXXXX U+XXXX
其中
HEXDIG
在引用RFC 4234中定义为HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
其中仅包含大写字母。
FWIW,根据我的研究,大多数JSON解析器都接受大写和小写字母。
问题:实际正确的是什么?是否存在矛盾,并且RFC中的语法应该固定?
最佳答案
我认为这是RFC 4234的这一部分所解释的:
ABNF strings are case-insensitive and the character set for these strings is us-ascii.
Hence:
rulename = "abc"
and:
rulename = "aBc"
will match "abc", "Abc", "aBc", "abC", "ABc", "aBC", "AbC", and "ABC".
另一方面,后续部分并不十分清楚:
To specify a rule that IS case SENSITIVE, specify the characters individually.
For example:
rulename = %d97 %d98 %d99
or
rulename = %d97.98.99
在
HEXDIG
规则的情况下,它们是以-开头的单个字符,但是从字面上将它们指定为"A"
等,而不是%d41
,因此我怀疑这意味着它们不区分大小写。这不是我读过的最清晰的规范:(
关于JSON Unicode转义序列-是否小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214417/