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:2
Avg. Score:4.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.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 Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or 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

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Apr 06 12:01:25 2020 UTC