Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose new constant for JSON_ERROR_NON_BACKED_ENUM #8238

Closed
lcdennison opened this issue Mar 22, 2022 · 1 comment
Closed

Expose new constant for JSON_ERROR_NON_BACKED_ENUM #8238

lcdennison opened this issue Mar 22, 2022 · 1 comment

Comments

@lcdennison
Copy link

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:

php-src/ext/json/json.c

Lines 190 to 191 in ec53e17

case PHP_JSON_ERROR_NON_BACKED_ENUM:
return "Non-backed enums have no default serialization";

However, it seems that a public constant is not available as it is not currently included in the list of defined constants:

php-src/ext/json/json.c

Lines 74 to 85 in ec53e17

/* json error constants */
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_STATE_MISMATCH", PHP_JSON_ERROR_STATE_MISMATCH);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_RECURSION", PHP_JSON_ERROR_RECURSION);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_INF_OR_NAN", PHP_JSON_ERROR_INF_OR_NAN);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_UNSUPPORTED_TYPE", PHP_JSON_ERROR_UNSUPPORTED_TYPE);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_INVALID_PROPERTY_NAME", PHP_JSON_ERROR_INVALID_PROPERTY_NAME);
PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_UTF16", PHP_JSON_ERROR_UTF16);

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:

typedef enum {
PHP_JSON_ERROR_NONE = 0,
PHP_JSON_ERROR_DEPTH,
PHP_JSON_ERROR_STATE_MISMATCH,
PHP_JSON_ERROR_CTRL_CHAR,
PHP_JSON_ERROR_SYNTAX,
PHP_JSON_ERROR_UTF8,
PHP_JSON_ERROR_RECURSION,
PHP_JSON_ERROR_INF_OR_NAN,
PHP_JSON_ERROR_UNSUPPORTED_TYPE,
PHP_JSON_ERROR_INVALID_PROPERTY_NAME,
PHP_JSON_ERROR_UTF16,
PHP_JSON_ERROR_NON_BACKED_ENUM,
} php_json_error_code;

@iluuu1994
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants