gpt4 book ai didi

json - 为什么 JSON 编码 UTF-16 代理对而不是直接编码 Unicode 代码点?

转载 作者:行者123 更新时间:2023-12-04 19:00:27 30 4
gpt4 key购买 nike

To escape a code point that is not in the Basic Multilingual Plane, the character is represented as a twelve-character sequence, encoding the UTF-16 surrogate pair. So for example, a string containing only the G clef character (U+1D11E) may be represented as "\uD834\uDD1E".



ECMA-404: The JSON Data Interchange Format

我相信 there is no need to encode this character at all ,所以可以直接表示为 "𝄞" .但是,如果希望对其进行编码,则必须按照规范将其编码为 "\uD834\uDD1E" , 不像 "\u1d11e" 那样(看起来很合理) .为什么是这样?

最佳答案

JSON 的关键架构特性之一是 JSON 编码的对象是有效的 Javascript 文字,可以使用 eval 进行评估。功能,例如。不幸的是,较旧的 Javascript 实现仅支持 16 位 Unicode 转义序列,在字符串文字中包含四个十六进制字符,因此除了以可移植的方式在转义序列中使用 UTF-16 代理来处理高于 0xFFFF 的代码点外,别无他法。 (允许任意代码点的 \u{...} 语法仅在 ECMAScript 6 中引入。)

但正如您所提到的,如果您的应用程序支持 Unicode JSON 文本,则无需使用转义序列。只需直接以相应的 Unicode 格式对字符进行编码。

关于json - 为什么 JSON 编码 UTF-16 代理对而不是直接编码 Unicode 代码点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38463038/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com