Skip to content

fix: escape lone surrogates in stringify - #384

Merged
jordanbtucker merged 1 commit into
json5:mainfrom
bybraveHQ:lone-surrogates
Oct 16, 2026
Merged

jordanbtucker merged 1 commit into
json5:mainfrom
bybraveHQ:lone-surrogates

Conversation

@bybraveHQ

Copy link
Copy Markdown
Collaborator

Fixes #192. stringify now escapes unpaired surrogate code units as \uXXXX, the same way JSON.stringify does since ES2019. Valid surrogate pairs are left intact, so the output is always well-formed Unicode and reads back as the same string.

The CLI case from the original report already works on current Node, because the CLI writes its output with JSON.stringify. I added a CLI test to keep it that way. The remaining bug was in JSON5.stringify, which returned the raw lone surrogate.

Tests cover lone high and low surrogates, a valid pair, a pair followed by a lone high surrogate, a reversed pair and a property name. npm test passes with 100% coverage.

@jordanbtucker
jordanbtucker merged commit 18f927c into json5:main Oct 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handling invalid unicode escapes

2 participants