From 2145f80d4bb76c3fe9739671300091265b6e20d6 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 31 Mar 2022 14:31:17 +0200 Subject: [PATCH] Register JSON_ERROR_NON_BACKED_ENUM constant (#8285) Fixes GH-8238 --- NEWS | 1 + Zend/tests/enum/json_encode.phpt | 6 ++++++ ext/json/json.c | 1 + 3 files changed, 8 insertions(+) diff --git a/NEWS b/NEWS index 92ac43c9854..cb1e2375332 100644 --- a/NEWS +++ b/NEWS @@ -347,6 +347,7 @@ PHP NEWS - JSON: . Fixed bug #81532 (Change of $depth behaviour in json_encode() on PHP 8.1). (Nikita) + . Fixed bug GH-8238 (Register JSON_ERROR_NON_BACKED_ENUM constant). (ilutov) - LDAP: . Convert resource to object \LDAP\Connection. (Máté) diff --git a/Zend/tests/enum/json_encode.phpt b/Zend/tests/enum/json_encode.phpt index 12ed0d3d445..f1174db7ae8 100644 --- a/Zend/tests/enum/json_encode.phpt +++ b/Zend/tests/enum/json_encode.phpt @@ -26,6 +26,10 @@ enum CustomFoo implements JsonSerializable { function test($value) { var_dump(json_encode($value)); echo json_last_error_msg() . "\n"; + if (json_last_error() !== JSON_ERROR_NONE) { + echo json_last_error() . ' === ' . JSON_ERROR_NON_BACKED_ENUM . ":\n"; + var_dump(json_last_error() === JSON_ERROR_NON_BACKED_ENUM); + } try { var_dump(json_encode($value, JSON_THROW_ON_ERROR)); @@ -44,6 +48,8 @@ test(CustomFoo::Bar); --EXPECT-- bool(false) Non-backed enums have no default serialization +11 === 11: +bool(true) JsonException: Non-backed enums have no default serialization string(1) "0" No error diff --git a/ext/json/json.c b/ext/json/json.c index 2d7846a5dbe..c8f0ed5e93a 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -83,6 +83,7 @@ static PHP_MINIT_FUNCTION(json) 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); + PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_NON_BACKED_ENUM", PHP_JSON_ERROR_NON_BACKED_ENUM); return SUCCESS; }