8

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.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
SuperNova
  • 2,792
  • 2
  • 25
  • 34
  • 1
    did you checked the similar thread http://stackoverflow.com/questions/15659325/json-bigint-as-string-backporting – Malay M Jun 02 '15 at 07:34
  • It's somewhat confusing what you're asking. To summarise: you want to `json_decode` a data set, change some property, then `json_encode` it again; your problem is that large integers are either going to turn into floats or strings when doing so. Have I got that correct? – deceze Jun 02 '15 at 07:35
  • Yes, but the suggestion is to use a third party lib. I like keeping my applications small. – SuperNova Jun 02 '15 at 07:37
  • @deceze: Yes, you're correct. I my opinion json_decode should not convert bigint to string or float. The servers says "no" to invalid datatypes, when sending the modified object back. – SuperNova Jun 02 '15 at 07:39
  • You can get a good roundtrip at least using `JSON_BIGINT_AS_STRING` and `JSON_NUMERIC_CHECK`... http://3v4l.org/7NkrR – this is not good enough for your use case? – deceze Jun 02 '15 at 07:40
  • Ah, sorry, I see now why it isn't... – deceze Jun 02 '15 at 07:42
  • 1
    how about sending big integers as string in json? – num8er Jun 02 '15 at 07:45

2 Answers2

5

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I'm using windows 8.1 (64 bit), apache v2.4 (64 bit) and php v5.6.9 (64 bit). – SuperNova Jun 02 '15 at 08:39
  • 2
    Well, caveat: [*"64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit."*](http://php.net/manual/en/language.types.integer.php) – deceze Jun 02 '15 at 08:40
  • Uhm, I think there is no solution. But I'd like you to earn the reputation. Thank you. My production system uses linux and it's fine there. – SuperNova Jun 03 '15 at 06:11
  • What about even bigger ints like "38 digits of precision" – Petah Mar 08 '17 at 01:58
1

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.