fix: escape keys in pretty output and decode surrogate pairs - #73
Merged
Merged
Conversation
- 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
force-pushed
the
hotfix/qj-json-encoding
branch
from
September 16, 2026 04:24
bf73776 to
493d7da
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更
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 ./...全量通过。