-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
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:
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:
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:
Lines 42 to 56 in ec53e17
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; | |