Skip to content

fix: escape keys in pretty output and decode surrogate pairs - #73

Merged
swordqiu merged 1 commit into
yunionio:masterfrom
swordqiu:hotfix/qj-json-encoding
Sep 16, 2026
Merged

swordqiu merged 1 commit into
yunionio:masterfrom
swordqiu:hotfix/qj-json-encoding

Conversation

@swordqiu

Copy link
Copy Markdown
Member

变更

PrettyString 的键未转义

String() / buildString 一直用 quoteString(k) 写键,但 JSONDict.prettyString 直接写原文。键名含引号或换行时,美化输出会变成另一个文档,甚至不是合法 JSON。改为复用 quoteString

\uXXXX 代理对未合并

非 BMP 字符在 JSON 里写成 😀 这样的代理对。parseQuoteString 逐个转义解析后直接 utf8.EncodeRune,两个代理各自落成 U+FFFD,字符静默损坏,与 encoding/json 的结果不一致。现在识别高代理后紧跟的低代理并合并;孤立代理仍按 U+FFFD 处理(与标准库一致)。

测试

新增 encoding_test.go

  • TestPrettyStringKeys:含引号、换行、中文、空键的字典,美化输出的往返结果必须与原值相等,且必须是合法 JSON 文档
  • TestParseSurrogatePair:7 种转义组合(合法代理对、孤立高/低代理、代理后接普通文本等)逐一对齐 encoding/json 的结果,并验证重新解析输出稳定
  • TestQuoteString:转义后的字符串可被 encoding/json 还原为原值

go test ./... 全量通过。

- JSONDict.prettyString wrote dict keys verbatim, escape them the same
  way the compact output does
- parseQuoteString combines the two \uXXXX escapes of a surrogate pair
  into the character they encode
@swordqiu
swordqiu force-pushed the hotfix/qj-json-encoding branch from bf73776 to 493d7da Compare September 16, 2026 04:24
@swordqiu
swordqiu merged commit c185bf1 into yunionio:master Sep 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant