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

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.

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

php serialize huge float causes rounding and formatting issues

I have the following php snippet
$newData = serialize(array('ep' => 50733372961735.4));
echo "New data: " . print_r($newData, 1);
Output:
New data: a:1:{s:2:"ep";d:5.07333729617E+13;}
But I would like the float value as it is and not E+13.
What could I do without having to make drastic changes as this is just an example. In my actual code the 'ep' value could be inside a complex array hierarchy
Firstly, a general note: serialize should never be used on data that could be manipulated in any way. It's useful for things like session data and caches, but should not be relied on for transporting data between applications or data storage. In many cases, you're better off using a standard serialization format like JSON.
You also certainly shouldn't care what the serialized string looks like - the only thing you should do with that string is pass it back to unserialize(). So the fact that there is E+13 is not a problem if the actual value it gives back when you unserialize is the one you wanted.
However, it's clear in your example that you have lost precision - the last digits are ...29617 rather than ...29617354 - so back to the point: there is a PHP setting serialize_precision, described in the manual here. It's default value has varied over the years, but setting it to an explicit value other than -1 will serialize floats with that number of significant figures:
ini_set('serialize_precision', 2);
echo serialize(50733372961735.4), PHP_EOL;
// d:5.1E+13;
ini_set('serialize_precision', 20);
echo serialize(50733372961735.4), PHP_EOL;
// d:50733372961735.398438;
Note that the first example has clearly thrown away information, whereas the second has actually stored more precision than you realised you had - because of the inaccuracy of storing decimals in binary floating point format.
The problem is not the serialize at all!
it's numbers in general.
$num = 50733372961735.4;
print($num);
=> 50733372961735
to "solve" this problem you can use:
ini_set('serialize_precision', 15);
I just executed your code on www.writephponline.com
I got the below value if I did not put your value in string :- $newData = serialize(array('ep' => 50733372961735.4));
New data: a:1:{s:2:"ep";d:50733372961735.398;}
and this after adding it in string :- $newData = serialize(array('ep' => '50733372961735.4'));
New data: a:1:{s:2:"ep";s:16:"50733372961735.4";}

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 - Parse file of different value types

I'm trying to parse basically an ini file without using parse_ini_file for a few reasons. I have the file parsing perfectly into a multidimensional array and am able to search through it properly and everything. The problem that I am having, is that I want the file to be parsed based on the type. There are strings, floats and integers in the file and I need to store these as such rather than all as strings.
The users will need to be able to get the values stored, so basically they would do "Get Float from Header section with the Budget key" and this would return 4.5. However if they tried to get the string or integer from the same section it would fail. I have it able to get the keys, but since they are all stored as strings that is the only thing that works. I'm wondering what how to change the type of the value from string to integer or float.
Anyone have any ideas how to do this?
It would be very difficult to accurately predict what type of value you are looking at. Strings could contain numbers, decimal points appear in sentences, etc...
If you are dealing with a custom ini file that you are generating for your own use, you might be able to encode the type of value into the ini settings. A standard setting may look like this -
default_username=Zamereon
So you could append or prepend the data type to the setting -
(s)default_username=Zamereon // one character depicting the data type,
(i)default_reputation=1 // you could use strpos.
default_balance=0.5=f // use list($name,$value,$type) = explode('=',$setting)
References -
strpos()
list()
explode()
Another suggestion would be to leave them as strings in your parsed multidimensional array and only convert them when you actually need them. Within a function or line of code dealing with some inputted data from the ini file, you'll know exactly what data type you'll need, if it's a float just cast it to a float as you need it. There's not real need to cast every variable to it's correct form right away because their value will not change in it's textual representation; The conversion only truly needs to take place before performing some further manipulations.
You can use type casting
Type casting in PHP works much as it does in C: the name of the
desired type is written in parentheses before the variable which is to
be cast.
<?php
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
?>
The casts allowed are:
(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL (PHP 5)

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

Categories