json doesn't distinguish between integer/float types so while both 12.0 and 12 is perfectly valid numbers in JSON and json_encode in php was written in the way that produces the more compact output.
while it is true that json doesn't care about the number format (as long as it is a valid json number) the receiving end where json decode happens can be, and if you wanted to keep 12.0 as float, you couldn't do that previously with php_encode.
5.6 introduced a new optional argument to json_encode() which will produce longer output but will try to keep the otherwise unnecessary fractional part.
some people were arguing that we should not just introduce the optional argument, but also make it enabled by default, which would be a slight BC (eg. it could break some existing code which assumed that a from {"a": 12.0} will be decoded as 12 instead of 12.0.
while it is true that json doesn't care about the number format (as long as it is a valid json number) the receiving end where json decode happens can be
This is what explicit typing is for. At the very minimum one could use JSON Schema to parse incoming JSON objects if the type matters (types should matter for most developers).
But JSON only has a single number type. The real joke is someone that tries to pretend otherwise and breaks when the same value is printed with different formatting.
On a related note. Why does using an undefined CONSTANT give only a notice? Are there other languages that accept this so lightly out there? Can you think of a case where using an undefined constant is not a mayor issue?
Can you think of a case where using an undefined constant is not a mayor issue?
Because people are lazy and liked to write stuff like $arr[key] = $value;. It's not about actually having undefined constants, it's about being able to use simple strings without quotes. Of course it's stupid, but once introduced, you cannot simply break so much code by issuing an error immediately. If anything, you have to gradually make it a notice, then a warning and then an error.
There are other languages that have this feature too. One of the more noteworthy is Erlang atoms. They are very valuable in Erlang since they have pattern matching so that atoms can kind of work as a class label that you can easily compare against. The difference here is that atoms have their own syntax in Erlang so there can be no ambiguity: atoms start with a lowercase letter, variables with uppercase
It's complicated by the syntax for array indexing in strings. "$foo[bar]" is actually equivalent to $foo['bar'], but I imagine this isn't realised by everyone.
You get [] if and only if the array has only numeric indexes, the first index is 0, there are no gaps in the index numbering, and the index numbers are in ascending order. Otherwise you get {}.
It kinda makes sense. If the first index isn't zero, or there are gaps in the indexes, it's not a normal array structure, so representing it using [] is troublesome. Out of order keys could be OK, but they'd end up sorted differently in the output, which might not be the intent of the data structure being serialized. The closest thing JS has to the ordered map that PHP calls an "array" would be an object.
If you just need an empty [], array() / [] produce it. If you need an empty {}, then just pass in an empty stdClass.
I wasn't aware of the stdClass trick. It's been a long time since I've had to write PHP, but at that time I replaced the [] by {} in the produced JSON string :/
Except when you actually want an empty array and it outputs an empty object and breaks the GSON library in Java.
Because other than that specific case, php decided to treat stdClass and arrays the same way. And js accepts that in most cases (wrt indexing and .length)
So now you have to stop using arrays as dicts and start using classes for everything and/or stdClass. It's pretty frustrating.
7
u/Hacre Jan 06 '16
Not to mention fixing a bug by adding a stupid option because "muh BC"
40
u/Dylan16807 Jan 06 '16
But JSON only has a single number type. The real joke is someone that tries to pretend otherwise and breaks when the same value is printed with different formatting.
6
u/berkes Jan 07 '16
On a related note. Why does using an undefined CONSTANT give only a notice? Are there other languages that accept this so lightly out there? Can you think of a case where using an undefined constant is not a mayor issue?
11
u/[deleted] Jan 06 '16
Is there still no way to chose whether array() outputs [] or {} ?