Comments (17)
Nette supports only UTF-8.
You can create your own response, send a different encoding and encode json without flag JSON_UNESCAPED_UNICODE, but it should not be part of Nette.
from application.
Are you saying that there is missing charset=UTF-8? So please send PR and set 'utf-8' here, setContentType($this->contentType, 'utf-8')
from application.
Yes, that is exactly what I am saying. I will make a PR over the weekend.
from application.
This is more of a Chrome bug than Nette bug because
- encoding of JSON can be reliable determined from first few bytes
- only UTF-8 is used in real-world
- UTF-8 is the default encoding
Also the official registration of mime type application/json
pretty much says that the charset
parameter is pointless for JSON.
from application.
Try this:
$this->payload = ["test"=>"ÝŽáýžřáýž"];
$this->sendPayload();
and you will see Chrome bug.
In real world are used ALL possible encodings. Even the impossible ones.
from application.
@dg Furthermore, there is a possible bug in Nette\Http\Repsonse, where you can set the charset by callingNette\Http\Response::setContentType('application/json; charset=utf-8', null)
instead of Nette\Http\Response::setContentType('application/json', 'charset=utf-8')
from application.
https://bugs.chromium.org/p/chromium/issues/detail?id=438464
from application.
Well this is just wrong :-( There is no "charset" parameter for the application/json content-type and JSON must be unicode by definition. Setting a charset to an application/json response may break things, like Nittro for example, because some people are trying to follow standards and specs and those specifically say there is no charset for application/json...
from application.
Besides, if you read through the chromium bug report, this is an edge case at best..
- It only happens to people who have a specific encoding set in their browser, instead of automatic encoding detection; yes, there still might be some people like that, but
- It only happens if you're looking directly at the JSON data out of context, i.e. by directly opening an API endpoint by typing its address in your browser address bar. If you load the JSON data, say, by AJAX, from a page that already is in UTF-8, then in that page's context the JSON data is interpreted correctly, as demonstrated here (yes, it's Opera, but the behaviour when opening the JSON URLs directly is the same). And who is the only person that will ever see the raw JSON data out of context? Yes, the developer.
So basically what you're arguing is that Nette should output invalid, standards-incompatible responses for the sake of a couple of developers who aren't even able to switch encoding to automatic detection. Neat.
from application.
@jahudka can you please post a link for the mentioned standards? Anyway, this should be probably reverted as a bug.
from application.
@fprochazka the IANA application/json
Media Type Registration explicitly says:
Required parameters: n/a
Optional parameters: n/a
...
Note: No "charset" parameter is defined for this registration.
Adding one really has no effect on compliant recipients.
And the last bit about a charset parameter not having an effect on compliant recipients is because JSON must always be in Unicode and the specific Unicode charset can be determined by looking at the first two characters, according to IETF RFC4627:
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
Since the first two characters of a JSON text will always be ASCII characters [RFC0020], it is possible to determine whether an octet stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking at the pattern of nulls in the first four octets.
00 00 00 xx UTF-32BE 00 xx 00 xx UTF-16BE xx 00 00 00 UTF-32LE xx 00 xx 00 UTF-16LE xx xx xx xx UTF-8
There is a little more to it since RFC7159 obsoleted RFC4627, but the end result is the same. See the comments below this SO answer.
from application.
Agreed, this change is controversial at least. If someone needs to set charset explicitly, they are free to do so.
Anyway, I just checked some of the top sites and their API Content-Type:
GitHub API: application/json; charset=utf-8
Bitbucket API: application/json; charset=utf-8
Facebook Graph API: application/json; charset=UTF-8
Twitter API: application/json; charset=utf-8
Youtube Data API: application/json; charset=UTF-8
from application.
@Majkl578 As my father was always prone to say in my childhood: "I don't care what your classmates' grades are, it's your grades I'm concerned about" :-) The fact that everybody else is doing it wrong doesn't mean we should be copying them.
from application.
@jahudka please, just admit you have written something what's crystal clear with the specs, but doesn't work in the real world.
edit: I do not argue about reverting, I just see your arguments quite weird.
from application.
@hrach What's so weird about trying to follow the specs? Admittedly my code could be more fault-tolerant about it's input (and I've already fixed that), but that's hardly relevant to the topic of Nette following the specs.. I've always found that the best philosophy for developing software is to be as tolerant about your input as you can get, while being as strict about your output as possible. I'm not saying I'm following that rule flawlessly, as this issue clearly proves; but that doesn't mean I don't (or shouldn't) try..
from application.
From my point of view - following specs is not how the internet actually works. The real world apps (definitely) doesn't follow the spec for a reason. We use features from css, html, js, ... which are hadly defined in specs. And someday, specs will have to reflect the current state. I do not claim following the specs is wrong, I'm saing it's not (should not be) the (major) reason why something should be done.
The question is: does it more break things or fix things?
I do not know the answer so I don't have an opinion.
from application.
Yes, it's true that in a lot of cases we don't, and can't, follow the specs, because there either aren't any, or they aren't adopted widely enough, or they're just stupid. I'm not saying that the specs are the Holy Writ and that they are the single source of ultimate truth. What I am saying is that there is a reason why programmers have come up with the idea of having specs in the first place, and that reason ultimately does make sense; and similarly that a lot of the specs that we do have were issued for a reason and as long as that reason makes sense, we should try to follow the specs, for the same reasons that we have them. Yes, it's a valid point that we should be looking at whether our code fixes more things than it breaks, but I think that in cases like this where there is not much evidence either way we should stick to the specs, because that should make our code less likely to break something in the future.
from application.
Related Issues (20)
- Domain in the routing debug panel
- Persistent typed property must not be accessed before initialization
- Unable to use custom TemplateFactory with LatteExtension since 3.1
- Error: Typed property $templateFactory is accessed before initialization HOT 1
- Methods like sendResponse() should contain never-return typehint HOT 3
- TechEmPower Benchmarks and Asynchronous Processes HOT 3
- Nette Request doesn't have FETCH API fix HOT 2
- `UnexpectedValueException` with `$throw` = false on presenter->getComponent
- Route constructUrl incorrectly builds a URL on a non-standard port HOT 1
- BC break in patch version: Component::link() signature
- Routing panel throws exception from UrlScript HOT 1
- Invalid type of parameter in url throws InvalidLinkException HOT 1
- Session-less store/restoreRequest HOT 2
- Persistent attribute used for persistent controls does not allow params with control names
- Combination of nette/application 3.1.7 and nette/utils 3.2.7 triggers a deprecation error in Tracy HOT 7
- Ugly look of Tracy routing bar
- Missing barebone JS library for AJAX/snippet HOT 16
- Link with captured variable and latte strict mode not working HOT 6
- Flash sessions - deprecated getter
- Nette Application 3.1.12 breaks compatibility with Latte 2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from application.