php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81344 JSON_NUMERIC_CHECK Behaviour
Submitted: 2021-08-10 12:32 UTC Modified: 2021-08-13 15:22 UTC
From: tobiasoitzinger at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 8.0.9 OS: Ubuntu 20.04
Private report: No CVE-ID: None
 [2021-08-10 12:32 UTC] tobiasoitzinger at gmail dot com
Description:
------------
When using JSON_NUMERIC_CHECK with json_encode, the behaviour differs starting from php8. I couldn't find if this is documented somewhere, i am also not sure if this is a bug. 

json_encode currently has no option to specify if leading zeros schould be kept. See:
https://bugs.php.net/bug.php?id=79189

This might be correct, since according to the JSON specification, a number cannot start with 0, there is also a bug for this:
https://bugs.php.net/bug.php?id=70680

If some wanted to keep the leading zero, a workaround could be used to add a space character after the number that should keep it's leading zeros.

In php < 8 using JSON_NUMERIC_CHECK:

1. with a whitespace after the number e.g "01234 " it would convert to (string)01234

2. without a whitespace after the number e.g "01234" it would convert to (int)1234

In php >= 8 using JSON_NUMERIC_CHECK:

Whitespaces do not matter, it always converts it to (int)1234.

The behaviour changed, which might be correct. However as stated above i couldn't find this in the changelog or somewhere else documented, maybe it is a bug / unexpected behaviour?


Test script:
---------------
print_r(json_encode('01234 ', JSON_NUMERIC_CHECK));

Expected result:
----------------
"01234 "

Actual result:
--------------
1234

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-10 12:43 UTC] nikic@php.net
This is due to https://wiki.php.net/rfc/saner-numeric-strings, which allowed trailing whitespace in numeric strings (leading whitespace was allowed previously).

I'm inclined to close this as "Not a Bug" on the premise that you explicitly opted into using magic, so magic is what you got. The correct usage is to not enable JSON_NUMERIC_CHECK and instead provide json_encode() with correct types in the first place, which allows you to precisely control where you want to use numbers and where you want to use strings. JSON_NUMERIC_CHECK perform a heuristic conversion, and the heuristic makes a choice that is undesirable for your use case.

The alternative here would be to use a stricter check for JSON_NUMERIC_CHECK than PHP's general notion of what a "numeric string" is.
 [2021-08-10 12:55 UTC] tobiasoitzinger at gmail dot com
Probably this is not a bug. As you stated it was more of a mistake on our side using the "magic", and it seems to be correct the way it behaves since php8+. As said just wasn't sure because i couldn't find why it beahves like that. Thanks for clearing things up.
 [2021-08-13 15:22 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-08-13 15:22 UTC] nikic@php.net
Closing this per above comments. I think we're better of sticking with the "standard" behavior here, even if it's not always the desired one.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 17:01:35 2024 UTC