Repository navigation
Conversation
JSON.stringify reads Number and String wrapper objects in a replacer array as property names, and unwraps them when they are passed as the space argument. Both were ignored here, so the output differed from JSON.stringify even though the replacer and spacer are documented to work the same.
BridgeAR
reviewed
Nov 18, 2026
BridgeAR
left a comment
Owner
There was a problem hiding this comment.
I am hesitant to implement this change, since using boxed primitives is really something that should never be done.
Is this a real use case you ran into?
The current list of exceptions already lists boxed primitives and that applies to everything as such.
Contributor
Author
|
No, not a real use case. I found it by comparing the output with |
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.
The readme says the replacer and spacer work the same as with
JSON.stringify(). That is not true for Number and String objects:The spec (SerializeJSONProperty / JSON.stringify steps 4 and 5) reads a Number or String object in the replacer list with
ToString, and unwraps a Number or String object passed asspacebefore it is used.The fix:
getUniqueReplacerSetalso keeps Number and String objects (asString(value)).stringifyunwraps a Number or String objectspacebefore it builds the spacer.Number.prototype.valueOf/String.prototype.valueOf, so objects from another realm work too, and plain objects that only inherit fromNumber.prototypeorString.prototypeare still ignored, like inJSON.stringify.Boxed primitives as values are untouched; that difference stays documented in the readme.
Tests: two new tests compare the output with
JSON.stringifyfor wrapper objects (also fromvm.runInNewContext) and for look-alike objects. They fail without the change.npm test(standard + tap, 100% coverage) andnpm run tscpass.