Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializes unicode strings differently than JSON.stringify #84

Open
justin-together opened this issue Apr 27, 2023 · 1 comment
Open

Comments

@justin-together
Copy link

JSONbig.stringify escapes '\u202c' while JSON.stringify does not:

Welcome to Node.js v16.20.0.
Type ".help" for more information.
> JSONbig = require('json-bigint')
[Function (anonymous)] {
  parse: [Function (anonymous)],
  stringify: [Function (anonymous)]
}
> x = { k: 'a\u202cz' }
{ k: 'a‬z' }
> [...JSON.stringify(x)].map(i => i.charCodeAt(0).toString(16))
[
  '7b', '22',   '6b',
  '22', '3a',   '22',
  '61', '202c', '7a',
  '22', '7d'
]
> [...JSONbig.stringify(x)].map(i => i.charCodeAt(0).toString(16))
[
  '7b', '22', '6b', '22',
  '3a', '22', '61', '5c',
  '75', '32', '30', '32',
  '63', '7a', '22', '7d'
]
>

Note the absence of 202c. In its place is the escaped string "\u202c".

More parameters controlling escaping would be helpful.

@justin-together
Copy link
Author

justin-together commented Apr 28, 2023

It seems that JSON.stringify() only escapes \u0000-\u001f:

let i = 0
for (i = 0; i <= 0x9999; i++) {
  const code = i.toString(16).padStart(4, '0')
  const str = JSON.parse(`"\\u${code}"`)
  const encoding = [...JSON.stringify(str)].map(x => x.charCodeAt(0).toString(16))
  if (encoding.length > 3) {
     console.log(code, encoding)
  }
}

I will try testing the change:

-        escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
+        escapable = /[\\\"\x00-\x1f\u0000-\u001f]/g,

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

No branches or pull requests

1 participant