Bug #79958 [Opn->Nab]: json_encode does not support JSON_BIGINT_AS_STRING

From: Date: Wed, 12 Aug 2020 06:53:04 +0000
Subject: Bug #79958 [Opn->Nab]: json_encode does not support JSON_BIGINT_AS_STRING
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228525@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79958&edit=1

 ID:                 79958
 Updated by:         sjon@php.net
 Reported by:        jmuraca at gmail dot com
 Summary:            json_encode does not support JSON_BIGINT_AS_STRING
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   windows (all?)
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

it seems you are missing some context in your report - the numbers you use for your examples exceed
the 64 bit maximum in PHP (see PHP_INT_MAX).

Given that PHP cannot process or print integers that big, it switches to scientific notation which
is better then nothing.

You might have an interesting feature-request when encoding for a 32 bit system


Previous Comments:
------------------------------------------------------------------------
[2020-08-12 00:31:09] jmuraca at gmail dot com

Description:
------------
json_encode should be able to accept the parameter JSON_BIGINT_AS_STRING to display large numbers as
string and/or numeric rather than scientific notation

my use case is a long number like that is used for some account management identifiers (my included
example is fake) and I want to json output the whole number - not a scientific number

Test script:
---------------
// correct, displays scientific notation
$data = array("val" => 1008901020901566350119);
var_dump(json_encode($data));

// correct, displays val as string
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data)); 

// correct, displays val as numeric
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data, JSON_NUMERIC_CHECK));

// ERROR, displays val as scientific
// ideally with JSON_BIGINT_AS_STRING val display as numeric and full number
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data, JSON_NUMERIC_CHECK|JSON_BIGINT_AS_STRING));   

// ERROR, displays val as scientific
// ideally with JSON_BIGINT_AS_STRING val display as numeric and full number
$data = array("val" => 1008901020901566350119);
var_dump(json_encode($data, JSON_BIGINT_AS_STRING));

Expected result:
----------------
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data, JSON_NUMERIC_CHECK|JSON_BIGINT_AS_STRING));   
// actual: 1.0089010209015663e+21
// expected: 1008901020901566350119



$data = array("val" => 1008901020901566350119);
var_dump(json_encode($data, JSON_BIGINT_AS_STRING));
// actual: 1.0089010209015663e+21
// expected: 1008901020901566350119



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79958&edit=1


Thread (8 messages)

« previous php.bugs (#228525) next »