14

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

Is that JSON string UTF-16 encoded?

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

2
  • 3
    Strings aren't in UTF-8 or UTF-16 : only their binary representation resulting of an encoding is. Commented Dec 2, 2014 at 15:40
  • The assumption that utf-8 is more space efficient that utf-16 is not correct in some cases, code points above \0800 are more efficiently stored in utf-16 Commented Dec 2, 2014 at 15:49

1 Answer 1

9

JavaScript engines are allowed to use either UCS-2 or UTF-16.

So, yes, JSON.stringify() will return a string in whatever encoding your implementation uses for strings. If you were to find a way to change that encoding within the context of your script, it would no longer be a valid JavaScript string.

For serialising it over a network, though, I would expect it to automatically be transcoded into the character set of the HTTP request (assuming you're talking about HTTP). So if you send it via HTTP POST with a character set of UTF-8, your browser should transparently handle the transcoding of that data before it is sent.

Otherwise browsers would really struggle with character set handling.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for "does transparently handle". You should only ever need to specifiy an encoding when doing IO, and not tamper with it on your own.
If you happen to be using some API which deals with Uint8Array (raw bytes), then you can decode a UTF-8 array from a string (or in otherwords encode a string into a UTF-8 array) using new TextEncoder("utf-8").encode(JSON.stringify(object))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.