php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79189 json_encode with JSON_NUMERIC_CHECK and extra option
Submitted: 2020-01-29 09:32 UTC Modified: -
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: dalius dot ulevicius at gmail dot com Assigned:
Status: Open Package: JSON related
PHP Version: 7.4.2 OS: Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dalius dot ulevicius at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-29 09:32 UTC] dalius dot ulevicius at gmail dot com
Description:
------------
JSON_NUMERIC_CHECK does not respect the definition of numbers.

I think will be great to add extra option like (JSON_NUMERIC_LEADZERO), to do not trailing zeros at the lead.

Extra reported as a bug many years ago: https://bugs.php.net/bug.php?id=70680

Test script:
---------------
print_r(json_encode(array("shouldBeString0123456" => "0123456", "shouldBeString+0123456" => "+0123456", "isInt" => "123456"), JSON_NUMERIC_CHECK | JSON_NUMERIC_LEADZERO));

Expected result:
----------------
{
    "shouldBeString0123456":"0123456",
    "shouldBeString+0123456":"+0123456",
    "isInt":123456
}

Actual result:
--------------
{
    "shouldBeString0123456":123456,    // Bad with JSON_NUMERIC_CHECK 
    "shouldBeString+0123456":123456,   // Bad with JSON_NUMERIC_CHECK 
    "isInt":123456                     // Good
}

Patches

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC