note 126006 added to function.json-decode
| From: | Alien426 at main dot php dot net | Date: | Wed, 14 Apr 2021 09:11:32 +0000 |
| Subject: | note 126006 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216280@lists.php.net to get a copy of this message | ||
Browsers don't choke on integers _starting_ with BigInt (64 bits), but before that (53 bits).
The introduction of BigInt to modern browsers doesn't help much, when JSON handling functions
do not support it. So I am trying to remedy that. My approach is to handle the decoded array before
re-encoding it to a string:
<?php
function fix_large_int(&$value)
{
if (is_int($value) && $value > 9007199254740991)
$value = strval($value);
}
$json_str = '{"id":[1234567890123456789,12345678901234567890]}';
$json_arr = json_decode($json_str, flags: JSON_BIGINT_AS_STRING | JSON_OBJECT_AS_ARRAY);
echo(json_encode($json_arr)); //
{"id":[1234567890123456789,"12345678901234567890"]} (BigInt is already converted
to a string here)
array_walk_recursive($json_arr, 'fix_large_int');
echo(json_encode($json_arr)); //
{"id":["1234567890123456789","12345678901234567890"]}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 5.100.49.35)
----
Manual Page -- https://php.net/manual/en/function.json-decode.php
Edit -- https://main.php.net/note/edit/126006
Del: integrated -- https://main.php.net/note/delete/126006/integrated
Del: useless -- https://main.php.net/note/delete/126006/useless
Del: bad code -- https://main.php.net/note/delete/126006/bad+code
Del: spam -- https://main.php.net/note/delete/126006/spam
Del: non-english -- https://main.php.net/note/delete/126006/non-english
Del: in docs -- https://main.php.net/note/delete/126006/in+docs
Del: other reasons-- https://main.php.net/note/delete/126006
Reject -- https://main.php.net/note/reject/126006
Search -- https://main.php.net/manage/user-notes.php