Skip to content

fix: unwrap Number and String objects in replacer arrays and space - #84

Closed
bardrop wants to merge 1 commit into
BridgeAR:mainfrom
bardrop-oss:fix-boxed-replacer-and-space
Closed

bardrop wants to merge 1 commit into
BridgeAR:mainfrom
bardrop-oss:fix-boxed-replacer-and-space

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

The readme says the replacer and spacer work the same as with JSON.stringify(). That is not true for Number and String objects:

const stringify = require('safe-stable-stringify')

JSON.stringify({ a: 1, 1: 2 }, [Object('a')]) // '{"a":1}'
stringify({ a: 1, 1: 2 }, [Object('a')])      // '{}'

JSON.stringify({ a: 1 }, null, Object(2))     // '{\n  "a": 1\n}'
stringify({ a: 1 }, null, Object(2))          // '{"a":1}'

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 as space before it is used.

The fix:

  • getUniqueReplacerSet also keeps Number and String objects (as String(value)).
  • stringify unwraps a Number or String object space before it builds the spacer.
  • Wrapper objects are detected with Number.prototype.valueOf / String.prototype.valueOf, so objects from another realm work too, and plain objects that only inherit from Number.prototype or String.prototype are still ignored, like in JSON.stringify.

Boxed primitives as values are untouched; that difference stays documented in the readme.

Tests: two new tests compare the output with JSON.stringify for wrapper objects (also from vm.runInNewContext) and for look-alike objects. They fail without the change. npm test (standard + tap, 100% coverage) and npm run tsc pass.

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 BridgeAR left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bardrop

bardrop commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

No, not a real use case. I found it by comparing the output with JSON.stringify over random inputs. You're right that the boxed primitives exception covers this too, so I'll close this. Thanks for taking a look.

@bardrop bardrop closed this Nov 18, 2026
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.

2 participants