Repository navigation
Decode JSON UTF-16 surrogate pairs instead of emitting invalid UTF-8 - #342
Merged
Merged
Conversation
json_parse treated each \uD800–\uDFFF escape as a BMP scalar, so Python json.dumps / MCP-A2A emoji became two CESU-8 bytes that json_serialize then replaced with U+FFFD. Co-authored-by: Tyler Reckart <tylerreckart@users.noreply.github.com>
This was referenced Sep 17, 2026
Keep both Unreleased changelog entries. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
Author
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_3e6798d9-d1d1-42b0-9d31-328ccda8d24c) |
tylerreckart
enabled auto-merge (squash)
September 21, 2026 13:30
Keep both Unreleased changelog entries. Co-authored-by: Cursor <cursoragent@cursor.com>
Keep both Unreleased changelog entries. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Problem
json_parsetreated each\uXXXXescape as a BMP code point and emitted a 1–3 byte sequence. RFC 8259 encodes supplementary-plane scalars (emoji, etc.) as a UTF-16 surrogate pair. Pythonjson.dumps(defaultensure_ascii=True) and many MCP / A2A peers emit that form.The two 3-byte CESU-8 halves are not UTF-8.
json_serializealready rejects overlong / surrogate-range encodings and replaces them with U+FFFD, so a round-trip of"\uD83D\uDE00"(😀) became two replacement characters. MCP tool results, A2A message parts, and HTTP bodies that used escaped emoji were corrupted before they reached the model.Fix
Decode a high+low surrogate pair into one scalar and emit well-formed UTF-8 (4 bytes for the SMP). Unpaired surrogates become U+FFFD, matching the serializer's replacement policy. BMP
\u00e9-style escapes are unchanged.Tests
unit_json— BMP escape,\uD83D\uDE00/ lowercase pair → 😀, serialize round-trip, unpaired high/low, high+BMP, nested MCP-shaped object, truncated hex. 9/9 passed locally (+ ASan/UBSan).Independently mergeable against
main(0456350). Code hunks (json.cpp, newtest_json.cpp,CMakeLists.txtafterunit_logger) do not overlap #321–#341. CHANGELOG[Unreleased]will need a rebase after whichever sibling merges first.Note
Low Risk
Localized parser change with dedicated tests; improves interoperability for MCP/A2A JSON without altering number/object parsing or API contracts.
Overview
Fixes
json_parseso\uXXXXhandling matches RFC 8259: adjacent UTF-16 high+low surrogate pairs decode to one supplementary-plane scalar and are emitted as valid UTF-8 (e.g.\uD83D\uDE00→ 😀). Previously each half was encoded as its own 3-byte sequence, whichjson_serializethen replaced with U+FFFD—breaking emoji in Pythonjson.dumps, MCP tool payloads, and A2A message shapes on parse/round-trip.Refactors
\uparsing withparse_hex4andappend_utf8(4-byte UTF-8 for SMP; unpaired or out-of-range surrogates → U+FFFD, aligned with the serializer). Addsunit_json/tests/test_json.cpp(BMP, pairs, serialize round-trip, nested MCP-style JSON, truncated hex) and a CHANGELOG entry under[Unreleased].Reviewed by Cursor Bugbot for commit 84fa86c. Bugbot is set up for automated code reviews on this repo. Configure here.