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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 03:01:28 2024 UTC