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

Converting XML to JSON does not escape invalid string characters #44

Closed
caminad opened this issue Jul 29, 2020 · 0 comments
Closed

Converting XML to JSON does not escape invalid string characters #44

caminad opened this issue Jul 29, 2020 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@caminad
Copy link

caminad commented Jul 29, 2020

Calling convert(xml, { format: 'json' }) on XML that includes a text node containing unescaped control characters produces JSON containing those characters:

> console.log(convert('<a>b\nc</a>', { format: 'json' }))
{"a":"b
c"}

Similarly other invalid characters are not escaped when emitting JSON. According to the spec, %x00-19, \ and " must be escaped in JSON strings.

To Reproduce

const { convert } = require('xmlbuilder2');

const invalidChars = Array.from(Array(0x20), (_, i) => {
  return String.fromCharCode(i);
}).concat('"', '\\');

const xml = convert(
  `<a>${invalidChars.map((c) => `<b>_${c}_</b>`).join('\n')}</a>`
);

const json = convert(xml, { format: 'json' });

JSON.parse(json); // throws

Expected behavior

JSON.parse(json); // should not throw

const obj = convert(xml, { format: 'object' });

JSON.stringify(obj) === json; // should be true

Version:

  • node.js: v14.6.0
  • xmlbuilder2 2.2.0@master

Additional context

I hit this issue when serializing responses from a SOAP endpoint which contain newline characters within embedded escaped HTML (something like <desc>&lt;p&gt;First&lt;/p&gt;\n&lt;p&gt;Second&lt;/p&gt;</desc>). I was initially doing JSON.stringify(convert(xml, { format: 'object' })) before I discovered this API, which seems like a reasonable workaround.

Thanks for this awesome library! ✨

@caminad caminad added the bug Something isn't working label Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants