summaryrefslogtreecommitdiff
path: root/ext/json/json_encoder.c
diff options
context:
space:
mode:
authorIlija Tovilo <ilija.tovilo@me.com>2020-06-10 23:10:18 +0200
committerIlija Tovilo <ilija.tovilo@me.com>2021-03-17 19:08:03 +0100
commit269c8dac1d56ee85d71ae94d9b28dd7d8e8de7b7 (patch)
tree810ac41b2157ff4e8063f9696f97e1a9d77837c4 /ext/json/json_encoder.c
parenta6fc427b8c51015c16541c112a26dd06bd75e99e (diff)
downloadphp-git-269c8dac1d56ee85d71ae94d9b28dd7d8e8de7b7.tar.gz
Implement enums
RFC: https://wiki.php.net/rfc/enumerations Co-authored-by: Nikita Popov <nikita.ppv@gmail.com> Closes GH-6489.
Diffstat (limited to 'ext/json/json_encoder.c')
-rw-r--r--ext/json/json_encoder.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c
index e78a6495e9..c76ddaf0fd 100644
--- a/ext/json/json_encoder.c
+++ b/ext/json/json_encoder.c
@@ -27,6 +27,7 @@
#include "php_json.h"
#include "php_json_encoder.h"
#include <zend_exceptions.h>
+#include "zend_enum.h"
static const char digits[] = "0123456789abcdef";
@@ -570,6 +571,19 @@ static int php_json_encode_serializable_object(smart_str *buf, zval *val, int op
}
/* }}} */
+static int php_json_encode_serializable_enum(smart_str *buf, zval *val, int options, php_json_encoder *encoder)
+{
+ zend_class_entry *ce = Z_OBJCE_P(val);
+ if (ce->enum_backing_type == IS_UNDEF) {
+ encoder->error_code = PHP_JSON_ERROR_NON_BACKED_ENUM;
+ smart_str_appendc(buf, '0');
+ return FAILURE;
+ }
+
+ zval *value_zv = zend_enum_fetch_case_value(Z_OBJ_P(val));
+ return php_json_encode_zval(buf, value_zv, options, encoder);
+}
+
int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
{
again:
@@ -606,6 +620,9 @@ again:
if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) {
return php_json_encode_serializable_object(buf, val, options, encoder);
}
+ if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) {
+ return php_json_encode_serializable_enum(buf, val, options, encoder);
+ }
/* fallthrough -- Non-serializable object */
case IS_ARRAY: {
/* Avoid modifications (and potential freeing) of the array through a reference when a