Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,11 @@ function configure (options) {
let stringifyFnReplacer = function (key, parent, stack, replacer, spacer, indentation) {
let value = parent[key]

if (typeof value === 'object' && value !== null && typeof value.toJSON === 'function') {
value = safe ? callSafe(value.toJSON, value, key) : value.toJSON(key)
if (typeof value === 'object' && value !== null) {
const toJSON = value.toJSON
if (typeof toJSON === 'function') {
value = safe ? callSafe(toJSON, value, key) : Reflect.apply(toJSON, value, [key])
}
}
value = replacer.call(parent, key, value)

Expand Down Expand Up @@ -394,9 +397,10 @@ function configure (options) {
if (value === null) {
return 'null'
}
if (typeof /** @type {ToJSON} */ (value).toJSON === 'function') {
const toJSON = /** @type {ToJSON} */ (value).toJSON
if (typeof toJSON === 'function') {
if (typeof key === 'number') key = String(key)
const jsonValue = safe ? callSafe(/** @type {ToJSON} */ (value).toJSON, value, key) : /** @type {ToJSON} */ (value).toJSON(key)
const jsonValue = safe ? callSafe(toJSON, value, key) : Reflect.apply(toJSON, value, [key])
if (typeof jsonValue !== 'object') {
return stringifyPrimitive(jsonValue)
}
Expand Down Expand Up @@ -485,9 +489,10 @@ function configure (options) {
if (value === null) {
return 'null'
}
if (typeof /** @type {ToJSON} */ (value).toJSON === 'function') {
const toJSON = /** @type {ToJSON} */ (value).toJSON
if (typeof toJSON === 'function') {
if (typeof key === 'number') key = String(key)
const jsonValue = safe ? callSafe(/** @type {ToJSON} */ (value).toJSON, value, key) : /** @type {ToJSON} */ (value).toJSON(key)
const jsonValue = safe ? callSafe(toJSON, value, key) : Reflect.apply(toJSON, value, [key])
if (typeof jsonValue !== 'object') {
return stringifyPrimitive(jsonValue)
}
Expand Down Expand Up @@ -602,9 +607,10 @@ function configure (options) {
if (value === null) {
return 'null'
}
if (typeof /** @type {ToJSON} */ (value).toJSON === 'function') {
const toJSON = /** @type {ToJSON} */ (value).toJSON
if (typeof toJSON === 'function') {
if (typeof key === 'number') key = String(key)
const jsonValue = safe ? callSafe(/** @type {ToJSON} */ (value).toJSON, value, key) : /** @type {ToJSON} */ (value).toJSON(key)
const jsonValue = safe ? callSafe(toJSON, value, key) : Reflect.apply(toJSON, value, [key])
// Prevent calling `toJSON` again
if (typeof jsonValue !== 'object') {
return stringifyPrimitive(jsonValue)
Expand Down
54 changes: 54 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,60 @@ test('circular depths can have multiple digits', /** @param {import('tap').Test}
assert.end()
})

test('toJSON accessors are read once', (assert) => {
const wrappers = [value => value, value => ({ item: value }), value => [value]]
for (const safe of [false, true]) {
const serializer = stringify.configure({ safe })
for (const inherited of [false, true]) {
for (const space of [undefined, 2]) {
for (const replacer of [undefined, ['item', 'value'], (key, value) => value]) {
for (const wrap of wrappers) {
const createInput = () => {
let reads = 0
const calls = []
const holder = Object.defineProperty({}, 'toJSON', {
get () {
reads++
/** @this {object} @param {string} key */
function toJSON (key) {
calls.push([this === value, key])
return { value: reads }
}
return toJSON
}
})
const value = inherited ? Object.create(holder) : holder
return { input: wrap(value), calls, readCount: () => reads }
}
const native = createInput()
const actual = createInput()
// @ts-expect-error Replacer covers both native overloads.
const expected = JSON.stringify(native.input, replacer, space)
assert.equal(serializer(actual.input, replacer, space), expected)
assert.equal(actual.readCount(), 1)
assert.same(actual.calls, native.calls, 'hook receives the original value and string key')
}
}
}
}
}
assert.end()
})

test('cached toJSON hooks can have their own call property', (assert) => {
/** @this {{value: number}} @param {string} key */
const toJSON = function (key) { return [this.value, key] }
toJSON.call = undefined
const input = { value: 1, toJSON }
for (const space of [undefined, 2]) {
for (const replacer of [undefined, ['value'], (key, value) => value]) {
// @ts-expect-error Replacer covers both native overloads.
assert.equal(stringify(input, replacer, space), JSON.stringify(input, replacer, space))
}
}
assert.end()
})

test('toJSON receives array keys as string', /** @param {import('tap').Test} assert */ (assert) => {
for (const safe of [false, true]) {
const serializer = stringify.configure({ safe })
Expand Down
Loading