Bug 251871 - JSON isn't implicitly treated as UTF-8
Summary: JSON isn't implicitly treated as UTF-8
Status: RESOLVED DUPLICATE of bug 197369
Alias: None
Product: WebKit
Classification: Unclassified
Component: Layout and Rendering (show other bugs)
Version: Safari 16
Hardware: All All
: P2 Normal
Assignee: Nobody
Depends on:
Reported: 2023-02-07 11:30 PST by Aaron Carson
Modified: 2023-02-08 19:32 PST (History)
3 users (show)

See Also:

Screenshot demonstrating Safari's behaviour with and without ;charset=utf-8 (717.44 KB, image/png)
2023-02-07 11:30 PST, Aaron Carson
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Aaron Carson 2023-02-07 11:30:54 PST
Created attachment 464889 [details]
Screenshot demonstrating Safari's behaviour with and without ;charset=utf-8


According to section 8 of RFC 8259[1], all JSON documents must be encoded using UTF-8 / Unicode.

If a JSON file is accessed directly within Safari (for example, an API endpoint is accessed), it uses the browser's default encoding (for me, it defaulted to `Western (ISO Latin 1)` to render it. This can lead to garbled text; I discovered this as an API endpoint I created contains two emoji characters which did not render correctly within Safari. In Chrome, it renders fine.

If the `Content-Type` HTTP header is set to `application/json;charset=utf-8`, it works fine in Safari. However, the inclusion of this is widely discouraged[2][3]. Not only that, IANA specifically call out that there is no `charset` parameter defined for the `application/json` media type[4].

So WebKit should automatically assume that `application/json` is in UTF-8 and render it as such. Otherwise, there will be more people who fall down a rabbit hole trying to work out what they've done wrong with character encoding when writing an API endpoint whilst using Safari!

[1] https://datatracker.ietf.org/doc/html/rfc8259#section-8
[2] https://github.com/request/request/issues/383
[3] https://stackoverflow.com/questions/70250068/with-fastapi-how-to-add-charset-to-content-type-media-type-on-request-header#:~:text=Short%20answer,not%20be%20necessary.
[4] https://www.iana.org/assignments/media-types/application/json
Comment 1 Aaron Carson 2023-02-07 11:34:05 PST
I may have misread #197369 as being about how JSON is processed in XMLHTTPRequests and/or iFrames but it seems it is a duplicate to a degree. Will leave as is currently though so maintainers can assess whether they're different at all.
Comment 2 Alexey Proskuryakov 2023-02-08 19:32:14 PST
I think that it's an exact duplicate.

*** This bug has been marked as a duplicate of bug 197369 ***