json_encode() Inf and NaN cannot be JSON encoded - php

I am encoding my array into JSON and below is the case in which it gives
Inf and NaN cannot be JSON encoded error
$test = ['key' => '6E01400'];
json_encode($test, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
I understand it considers 'E' in a key as an exponent and tries to convert into a number something like 6.0 e+1400...I don't know not sure though.
Is there anyway I can avoid that conversion for special case like this one, I really need to use JSON_NUMERIC_CHECK option here, any clue guys?

There is a workaround for this in PHP 5.5.0 and above so that you can still get JSON to output, but it does rewrite your data.
The JSON_PARTIAL_OUTPUT_ON_ERROR option can be set on json_encode, if you are getting back a JSON_ERROR_INF_OR_NAN. The PHP doc says that in the case of the NAN or INF value, "0 will be encoded in the place of these special numbers."

Related

Inf and NaN cannot be JSON encoded [duplicate]

Apparently, infinity and NaN are not a part of JSON specification, so this PHP code:
$numbers = array();
$numbers ['positive_infinity'] = +INF;
$numbers ['negative_infinity'] = -INF;
$numbers ['not_a_number'] = NAN;
$array_print = print_r ($numbers, true);
$array_json = json_encode ($numbers);
echo "\nprint_r(): $array_print";
echo "\njson_encode(): $array_json";
Produces this:
PHP Warning: json_encode(): double INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
PHP Warning: json_encode(): double -INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
PHP Warning: json_encode(): double NAN does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
print_r(): Array
(
[positive_infinity] => INF
[negative_infinity] => -INF
[not_a_number] => NAN
)
json_encode(): {"positive_infinity":0,"negative_infinity":0,"not_a_number":0}
Is there any way to correctly encode these numbers without writing my own json_encode() function? Maybe some workaround?
You are right about the JSON spec:
Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.
The solution must also come from the spec, since a custom "JSON" encoder would not produce valid JSON anyway (you would have to write a custom decoder as well, and then you and consumers of your data would be forced to use that until the end of time).
Here' what the spec allows for values:
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
false null true
So, any workaround that involves legal JSON instead of a custom JSON-like protocol would involve using something else instead of numbers.
One reasonable option would be to use the strings "Infinity" and "NaN" for these edge cases.
According to JSON spec, there is no Infinity or NaN values: http://json.org/
Workarounds:
Reject using JSON (pure JSON), and write your own json_encode function, which will handle INF/NAN (converting to "Infinity" and "NaN" respectively), and make sure you are parsing JSON using something like result = eval('(' + json + ')'); on the client side.
Pre convert your IFN/NAN values into string values ('Infinity' and 'NaN'), and when you are going to operate with those values in JavaScript, use the following construction: var number1 = (+numbers.positive_infinity);. This will convert string value 'Infinity' into numeric Infinity representation.
This is in my opinion a big shortcoming of JSON. Different JSON encoders handle this differently, a quick overview can be found here: http://lavag.org/topic/16217-cr-json-labview/?p=99058
One solution is to encode +inf as +1e9999 as that will naturally overflow to +inf in most decoders, and same with -inf as -1e9999. NaN is much harder.
As an update to readers of this question for newer versions of PHP >= 5.5.0, to get INF or NAN values from json_encode to be encoded as 0 rather than json_encode failing to output at all, add the JSON_PARTIAL_OUTPUT_ON_ERROR option.
As an example: json_encode($data, JSON_NUMERIC_CHECK | JSON_PARTIAL_OUTPUT_ON_ERROR);
The warning mentioned above, there is official bug reported in php documentation.
https://bugs.php.net/bug.php?id=64695

json_encode() converts large number to scientific notation [duplicate]

As noted in the PHP documentation, when json_decodeing a data structure containing long integers, they'll be converted to floats. The workaround is to use JSON_BIGINT_AS_STRING, which preserves them as strings instead. When json_encodeing such values, JSON_NUMERIC_CHECK will encode those numbers back into large integers:
$json = '{"foo":283675428357628352}';
$obj = json_decode($json, false, JSON_BIGINT_AS_STRING);
$json2 = json_encode($obj, JSON_NUMERIC_CHECK);
var_dump($json === $json2); // true
Using this method for a correct roundtrip of the data is prone to errors. If a property contains '123', a numeric string which should stay a string, it will be encoded to an integer.
I want to get an object from the server, modify one property and than put the entire data structure back. I need to preserve the original types. I don't want to maintain properties other than the one I'm manipulating.
Is there any real workaround for this? PHP does not have any issues with big ints anymore, but the json_decode routine seems to be outdated.
As long as your PHP version can actually handle large integers, meaning if you're running a 64-bit version of PHP (on something other than Windows), json_decode has no problem with it:
$json = '{"foo":9223372036854775807}';
$obj = json_decode($json);
$json2 = json_encode($obj);
var_dump(PHP_INT_MAX, $obj, $json2);
int(9223372036854775807)
object(stdClass)#1 (1) {
["foo"]=>
int(9223372036854775807)
}
string(27) "{"foo":9223372036854775807}"
If the integer values you need to handle do exceed PHP's PHP_INT_MAX, you simply cannot represent them in PHP native types. So there's no way around the conundrum you have; you cannot use native types to track the correct type, and you cannot substitute other types (e.g. strings instead of integers), because that's ambiguous when encoding back to JSON.
In this case you will have to invent your own mechanism of tracking the correct types for each property and handle such serialisation with a custom encoder/decoder. For example, you'd need to write a custom JSON decoder which can decode to a custom class like new JsonInteger('9223372036854775808'), and your custom encoder would recognise this type and encode it to a JSON 9223372036854775808 value.
There's no such thing built into PHP.
For what it's worth, PHP can support values > PHP_INT_MAX using the bcmath package http://php.net/manual/en/book.bc.php but JSON is a slightly more difficult issue.
To answer the OP's question of why they can't encode the value from a string back to an int type in the JSON, the answer lies in the conversion step. When reading the original JSON string in, it's a string, and read byte by byte. When reading values, they're initially read as a string (as the JSON itself if a string), and later cast to the correct type to an int or a float depending upon the presence of a period (.). If the value is greater than PHP_INT_MAX then PHP converts it to a double, and you lose precision. Thus using JSON_BIGINT_AS_STRING will tell the parser to keep the value as a string and NOT try to cast it, everything should be good, the value is kept in tact, albeit a string.
The problem comes when doing the inverse, and doing json_encode($value, JSON_NUMERIC_CHECK) tells PHP to cast string numeric values into either int/float, but this appears to happen BEFORE writing to the JSON string, causing values > PHP_INT_MAX to be converted into a double representation like 9.2233720368548e+19
See https://3v4l.org/lHL62 or below:
$bigger_than_max = '{"max": ' . PHP_INT_MAX . '1}'; // appending 1 makes > PHP_INT_MAX
var_dump($bigger_than_max);
var_dump(json_decode($bigger_than_max));
var_dump(json_decode($bigger_than_max, false, 512, JSON_BIGINT_AS_STRING));
var_dump(json_encode(json_decode($bigger_than_max, false, 512, JSON_BIGINT_AS_STRING)));
var_dump(json_encode(json_decode($bigger_than_max, false, 512, JSON_BIGINT_AS_STRING), JSON_NUMERIC_CHECK));
Result:
string(29) "{"max": 92233720368547758071}"
object(stdClass)#1 (1) {
["max"]=>
float(9.2233720368548E+19)
}
object(stdClass)#1 (1) {
["max"]=>
string(20) "92233720368547758071"
}
string(30) "{"max":"92233720368547758071"}"
string(29) "{"max":9.223372036854776e+19}"
Unfortunately, it doesn't appear that there is a way to solve this, looking at the JSON constants http://php.net/manual/en/json.constants.php I don't see anything that allows you to write integer values > PHP_INT_MAX into ints within the JSON.
Sorry this doesn't find a solution but hopefully clears up some confusion.

PHP JSON BigInt encoding

I have array like:
$array = ['id' => '76561198165327575'];
And I need it to work in JavaScript on client side. So I'm trying to encode it with JSON_NUMERIC_CHECK:
json_encode($array, JSON_NUMERIC_CHECK);
And getting result like:
{"id":7.6561198165328e+16}
But it should be:
{"id":76561198165327575}
What is wrong?
(Azure, Windows, 5.6)
JSON_NUMERIC_CHECK basically tells the encoder "If it looks like a number, encode it as a number":
php > $x = '123456789012234567890';
php > echo json_encode($x, JSON_NUMERIC_CHECK);
1.2345678901223e+20
php > echo json_encode($x);
"123456789012234567890"
And since your number exceeds the representable range for an INT on your platform, you get a float instead.
You are exceeding the bounds of integer on your 32-bit system. The documentation describes that when this occurs, the number is converted to a float.
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
If you encode in JSON objects with large numbers (greater than PHP_MAX_INT), you will always end up getting a floating point value. The only solution is to store them in the object/array as string (that you already) and not use JSON_NUMERIC_CHECK (but convert the string to a number on the client) or write your own encoding routine.

PHP: How to encode infinity or NaN numbers to JSON?

Apparently, infinity and NaN are not a part of JSON specification, so this PHP code:
$numbers = array();
$numbers ['positive_infinity'] = +INF;
$numbers ['negative_infinity'] = -INF;
$numbers ['not_a_number'] = NAN;
$array_print = print_r ($numbers, true);
$array_json = json_encode ($numbers);
echo "\nprint_r(): $array_print";
echo "\njson_encode(): $array_json";
Produces this:
PHP Warning: json_encode(): double INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
PHP Warning: json_encode(): double -INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
PHP Warning: json_encode(): double NAN does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
print_r(): Array
(
[positive_infinity] => INF
[negative_infinity] => -INF
[not_a_number] => NAN
)
json_encode(): {"positive_infinity":0,"negative_infinity":0,"not_a_number":0}
Is there any way to correctly encode these numbers without writing my own json_encode() function? Maybe some workaround?
You are right about the JSON spec:
Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.
The solution must also come from the spec, since a custom "JSON" encoder would not produce valid JSON anyway (you would have to write a custom decoder as well, and then you and consumers of your data would be forced to use that until the end of time).
Here' what the spec allows for values:
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
false null true
So, any workaround that involves legal JSON instead of a custom JSON-like protocol would involve using something else instead of numbers.
One reasonable option would be to use the strings "Infinity" and "NaN" for these edge cases.
According to JSON spec, there is no Infinity or NaN values: http://json.org/
Workarounds:
Reject using JSON (pure JSON), and write your own json_encode function, which will handle INF/NAN (converting to "Infinity" and "NaN" respectively), and make sure you are parsing JSON using something like result = eval('(' + json + ')'); on the client side.
Pre convert your IFN/NAN values into string values ('Infinity' and 'NaN'), and when you are going to operate with those values in JavaScript, use the following construction: var number1 = (+numbers.positive_infinity);. This will convert string value 'Infinity' into numeric Infinity representation.
This is in my opinion a big shortcoming of JSON. Different JSON encoders handle this differently, a quick overview can be found here: http://lavag.org/topic/16217-cr-json-labview/?p=99058
One solution is to encode +inf as +1e9999 as that will naturally overflow to +inf in most decoders, and same with -inf as -1e9999. NaN is much harder.
As an update to readers of this question for newer versions of PHP >= 5.5.0, to get INF or NAN values from json_encode to be encoded as 0 rather than json_encode failing to output at all, add the JSON_PARTIAL_OUTPUT_ON_ERROR option.
As an example: json_encode($data, JSON_NUMERIC_CHECK | JSON_PARTIAL_OUTPUT_ON_ERROR);
The warning mentioned above, there is official bug reported in php documentation.
https://bugs.php.net/bug.php?id=64695

How to convert base64 string to binary array using php

I have base 64 encoded string that looks something like this.
cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=
I have tried base64_decode and output is.
råkq‹å©ŒÞÜæ|c›R©6Ó™œªë´Áäw¸lø
I think I may be doing something wrong. I appreciate any help to convert base64 string to binary array.
Thanks
like this
$a = base64_decode("cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=");
$b = array();
foreach(str_split($a) as $c)
$b[] = sprintf("%08b", ord($c));
print_r($b);
You already are getting binary data back from base64_decode (if the encoded data was in fact binary), only this binary data is interpreted as encoding for some text by whatever you're outputting to (browser?). A "0011010110011001" output itself would also only be text, which would be encoded using some (different) binary stream. The computer does not work with 1's and 0's internally, contrary to popular believe. If you want to visualize binary data in the form of 1's and 0's, you'll need to do the binary/text conversion yourself. Usually that's a pretty pointless thing to do, though.
You're probably already doing the right thing. Your mistake is in expecting binary data to be represented as "0100101010".

Categories