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

AsciiJSON is incorrect w.r.t RFC4627 #2546

Open
rickb777 opened this issue Oct 31, 2020 · 0 comments
Open

AsciiJSON is incorrect w.r.t RFC4627 #2546

rickb777 opened this issue Oct 31, 2020 · 0 comments

Comments

@rickb777
Copy link

render/json.go defines AsciiJSON and some internal names jsonContentType, jsonpContentType, jsonAsciiContentType. This shows a misunderstanding of RFC4627 section 3: "JSON text SHALL be encoded in Unicode".

From the RFC, it follows

  • the content type application/json implies that the content is UTF-8 or UTF-16 or UTF-32 - the RFC describes how the decoder detects the correct UTF from the first bytes.
  • it is not helpful to use the term ASCII, even if the JSON content happens to be an ASCII-subset of UTF-8

AsciiJSON doesn't do what its name suggests. It should be deprecated (and later removed)

Because Go strings are UTF-8 by default, it is equally correct to use the content-type with charset (application/json;charset=utf-8) and without charset (application/json). The longer form takes a few more bytes but eliminates the (tiny) chance of there being a faulty decoder at the other end and it getting it wrong.

But if someone wants to force the encoding to be some other UTF (e.g. UTF-16 for Asian content), there isn't a way to do that in Gin and maybe that's what is needed. For Asian content, the advantage would be that the UTF-16 encoding would take fewer bytes (a bit less than two-thirds) than UTF-8 encoding for the same content.

So perhaps replace AsciiJSON with JSON_UTF16LE and JSON_UTF16BE. The render methods would need to ensure that the written content is correctly transcoded into the chosen UTF-16 encoding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant