# json_decode() and integers larger than LONG_MAX (2^31 - 1)

- Thread: https://externals.io/message/48364
- Original archive: https://news-web.php.net/php.internals/48364
- Started by Sara Golemon on 2010-05-20T19:50:37+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #48364 — Sara Golemon — 2010-05-20T19:50:37+00:00

URL: https://externals.io/message/48364#48364

I know this issue has been seen before, but I hope to do something about it.

$json = '1234567890123456789';
$x = json_decode($json);
/* $x is now a float *aproximately*
  * equal to the original value
  */

For database indexes, being off by one can be disasterous for 
application flow, so I'd like to include the option to decode very large 
integers as strings.

To that end, I propose a change in `json_decode()`'s signature to add an 
optional fourth argument, mirroring `json_encode()`'s second argument:

mixed json_decode(string $json[, bool $assoc = false[, int $depth = 
512[, int $options]]]);

Where $options is a bitmask of zero or more of the following:

JSON_BIGINT_STRING - Store large integers as their original string value

With the corresponding simple changes in json.c and JSON_parser.c

If noone objects or offers comments, I'll implement this.

-Sara

---

## #48365 — Pierre Joye — 2010-05-20T19:58:52+00:00

URL: https://externals.io/message/48364#48365 · In reply to: #48364

hi Sara,

The idea sounds sane, until we have big integer support :)

Only one thing, I would suggest to use `JSON_BIGINT_AS_STRING` instead,
more clear about its effect.

Cheers,


> I know this issue has been seen before, but I hope to do something about it.
>
> $json = '1234567890123456789';
> $x = json_decode($json);
> /* $x is now a float *aproximately*
>  * equal to the original value
>  */
>
> For database indexes, being off by one can be disasterous for application
> flow, so I'd like to include the option to decode very large integers as
> strings.
>
> To that end, I propose a change in `json_decode()`'s signature to add an
> optional fourth argument, mirroring `json_encode()`'s second argument:
>
> mixed json_decode(string $json[, bool $assoc = false[, int $depth = 512[,
> int $options]]]);
>
> Where $options is a bitmask of zero or more of the following:
>
> JSON_BIGINT_STRING - Store large integers as their original string value
>
> With the corresponding simple changes in json.c and JSON_parser.c
>
> If noone objects or offers comments, I'll implement this.
>
> -Sara
>
> --
> 
> 
>
>



-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #48366 — Ilia Alshanetsky — 2010-05-20T20:32:22+00:00

URL: https://externals.io/message/48364#48366 · In reply to: #48364

Sara,

Could you not treat a very large integer as a double? We do that in a few
areas of the code already.



> I know this issue has been seen before, but I hope to do something about
> it.
>
> $json = '1234567890123456789';
> $x = json_decode($json);
> /* $x is now a float *aproximately*
>  * equal to the original value
>  */
>
> For database indexes, being off by one can be disasterous for application
> flow, so I'd like to include the option to decode very large integers as
> strings.
>
> To that end, I propose a change in `json_decode()`'s signature to add an
> optional fourth argument, mirroring `json_encode()`'s second argument:
>
> mixed json_decode(string $json[, bool $assoc = false[, int $depth = 512[,
> int $options]]]);
>
> Where $options is a bitmask of zero or more of the following:
>
> JSON_BIGINT_STRING - Store large integers as their original string value
>
> With the corresponding simple changes in json.c and JSON_parser.c
>
> If noone objects or offers comments, I'll implement this.
>
> -Sara

---

## #48367 — Sara Golemon — 2010-05-20T21:22:36+00:00

URL: https://externals.io/message/48364#48367 · In reply to: #48366

Ilia Alshanetsky wrote:
> Could you not treat a very large integer as a double? We do that in a few
> areas of the code already.
> 
Treating it as a double is exactly what `json_decode()` *currently* does, 
and it's the problem I'm trying to work around by leaving it in string form.

When a large integer is cast as a double, it looses precision, and 
1234567890123456789 might suddenly look like (for example)

php -r 'var_dump((float)"1234567890123456789"
float(1.2345678901235E+18)

We've now lost the lowest four digits of this number.  For a 
sum/average/difference type number, that imprecision may be acceptable, 
but for a specific ID used in (for example) a database, it means that 
9,999 of every 10,000 records all wind up correlating to the same 
incorrect ID.

-Sara
