JSON Unicode转义序列-是否小写?

标签 json unicode language-lawyer rfc

我正在阅读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/

相关文章:

java - 解析 JSON 以与数组进行映射

assembly - 在 MASM32 程序集中打印 unicode 字符

python - 在 Python 中使用带有 LOCALE 和 UNICODE 标志的 Regex 测试空格

python - Unicode解码错误: 'utf8' codec can't decode byte - Euro Symbol

c++ - 是什么让 enum -> int 比 enum -> unsigned 转换更好?

c++ - C++ 中的声明符

c++ - 如何避免在const自动对象曾经使用的存储中创建任何对象?

xml - 如何使用Spring Boot Rest Data返回XML

java - 将存储为对象的字符串属性的 json 作为 JSON 对象发送到客户端

c# - 使用 httpclient 在 C# 中获取 API 数据