Rich-Harris/devalue

Escape non-ascii characters?

Conduitry opened this issue · 1 comments

Ref: sveltejs/v2.svelte.dev#236

This might be something that would be nice. It would have avoided the above issue. If the goal is to produce javascript to re-create the same object, it'd be cool if that javascript were resilient to charset issues.

I'm encountering a similiar problem in production as well. You get UGC data in your database, then you are playing russian roulette.

JSON.parse(devalue.stringify(decoder.write(Buffer.from([0x11]))))
Uncaught SyntaxError: Unexpected token  in JSON at position 2

//compared with
JSON.parse(JSON.stringify(decoder.write(Buffer.from([0x11]))))
'\x11'