JSON.stringify () - UTF-8

Javascript uses, as far as I know, UTF-16 mainly as a standard for strings. With JSON.stringify (), I can create a JSON string from an object.

Is this JSON UTF-16 encoding?

Can I convert (hopefully quickly) this string to UTF-8 to save bandwidth for huge files (1MB JSON)?

+3


source to share


1 answer


JavaScript engines can use UCS-2 or UTF-16.

So yes, JSON.stringify()

will return a string in whatever encoding your implementation uses for strings. If you found a way to change this encoding in the context of your script, it will no longer be a valid JavaScript string.



For serializing it over the network, however, I expect it to be auto-encoded into the HTTP request character set (assuming you're talking about HTTP). Therefore, if you send it via HTTP POST with the UTF-8 character set, your browser should transparently handle the transcoding of that data before sending it.

Otherwise browsers will really struggle with character set handling.

+1


source







All Articles