Skip to content

Commit

Permalink
fix: stringify \0 as \\x00 in some cases
Browse files Browse the repository at this point in the history
The following is invalid JSON5 and ES5 because a digit cannot follow a
null character escape.

    '\01'

However, JSON5 stringifies `'\x001'` as `'\01'` making it impossible to
round trip. This commit ensures that JSON5 stringifies null characters
as `\x00` when followed by a digit.
  • Loading branch information
jordanbtucker committed Oct 2, 2019
1 parent cdefab8 commit 3d9c22e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/stringify.js
Expand Up @@ -124,13 +124,20 @@ module.exports = function stringify (value, replacer, space) {

let product = ''

for (const c of value) {
for (let i = 0; i < value.length; i++) {
const c = value[i]
switch (c) {
case "'":
case '"':
quotes[c]++
product += c
continue

case '\0':
if (util.isDigit(value[i + 1])) {
product += '\\x00'
continue
}
}

if (replacements[c]) {
Expand Down
8 changes: 8 additions & 0 deletions test/stringify.js
Expand Up @@ -38,6 +38,10 @@ describe('JSON5', () => {
assert.strictEqual(JSON5.stringify({'\\\b\f\n\r\t\v\0\x01': 1}), "{'\\\\\\b\\f\\n\\r\\t\\v\\0\\x01':1}")
})

it('stringifies escaped null character property names', () => {
assert.strictEqual(JSON5.stringify({'\0\x001': 1}), "{'\\0\\x001':1}")
})

it('stringifies multiple properties', () => {
assert.strictEqual(JSON5.stringify({abc: 1, def: 2}), '{abc:1,def:2}')
})
Expand Down Expand Up @@ -129,6 +133,10 @@ describe('JSON5', () => {
assert.strictEqual(JSON5.stringify('\\\b\f\n\r\t\v\0\x0f'), "'\\\\\\b\\f\\n\\r\\t\\v\\0\\x0f'")
})

it('stringifies escaped null characters', () => {
assert.strictEqual(JSON5.stringify('\0\x001'), "'\\0\\x001'")
})

it('stringifies escaped single quotes', () => {
assert.strictEqual(JSON5.stringify(`'"`), `'\\'"'`)
})
Expand Down

0 comments on commit 3d9c22e

Please sign in to comment.