php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Expose new constant for JSON_ERROR_NON_BACKED_ENUM

Open lcdennison opened this issue 4 years ago • 1 comments

Description

When a non-backed enum is part of the data given to json_encode(), an integer error code (11) will be generated that is not currently available via JSON_ERROR_* constants. This error code seems to be internally recognized and supported (e.g. has a message) within PHP:

https://github.com/php/php-src/blob/ec53e17adf4f1669411334750a1d47954e2bc8e3/ext/json/json.c#L190-L191

However, it seems that a public constant is not available as it is not currently included in the list of defined constants: https://github.com/php/php-src/blob/ec53e17adf4f1669411334750a1d47954e2bc8e3/ext/json/json.c#L74-L85

My "feature request" is simply that this constant (JSON_ERROR_NON_BACKED_ENUM?) be added so we do not have to rely on the integer 11 to handle this error case.

--

Side note, it seems the 11 code is simply a result of that internal PHP_JSON_ERROR_NON_BACKED_ENUM being the 12th item (thus 11 index) in this enum list: https://github.com/php/php-src/blob/ec53e17adf4f1669411334750a1d47954e2bc8e3/ext/json/php_json.h#L42-L56

lcdennison avatar Mar 22 '22 04:03 lcdennison

This is an oversight, and I'd be ok labeling this as a bug.

iluuu1994 avatar Mar 22 '22 06:03 iluuu1994