Bug #81344 [Opn]: JSON_NUMERIC_CHECK Behaviour

From: Date: Tue, 10 Aug 2021 12:43:24 +0000
Subject: Bug #81344 [Opn]: JSON_NUMERIC_CHECK Behaviour
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235727@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81344&edit=1

 ID:                 81344
 Updated by:         nikic@php.net
 Reported by:        tobiasoitzinger at gmail dot com
 Summary:            JSON_NUMERIC_CHECK Behaviour
 Status:             Open
 Type:               Bug
 Package:            JSON related
 Operating System:   Ubuntu 20.04
 PHP Version:        8.0.9
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2021-08-10 12:32:03] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81344&edit=1


Thread (4 messages)

« previous php.bugs (#235727) next »