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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
41 - 40 = ?
Subscribe to this entry?

 
 [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 Oct 15 07:01:27 2024 UTC