Skip to content

Expose new constant for JSON_ERROR_NON_BACKED_ENUM #8238

Closed
@lcdennison

Description

@lcdennison

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;

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions