Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape JSON special chars. Fixes #44
- Loading branch information
Showing
3 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import $$ from '../TestHelpers' | ||
import { select } from "xpath" | ||
|
||
describe('Replicate issue', () => { | ||
|
||
// https://github.com/oozcitak/xmlbuilder2/issues/44 | ||
test('#44 - Converting XML to JSON does not escape invalid string characters', () => { | ||
const jsonStr = $$.convert('<a>b\nc</a>', { format: 'json' }) | ||
expect(jsonStr).toBe(`{"a":"b\\nc"}`) | ||
|
||
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' }) | ||
expect(() => JSON.parse(json)).not.toThrow() | ||
|
||
const obj = $$.convert(xml, { format: 'object' }) | ||
expect(JSON.stringify(obj)).toBe(json) | ||
}) | ||
}) |