com php-src: Fixed bug #72069 (Behavior \JsonSerializable different from json_encode): NEWS ext/json/json.c ext/json/json _encoder.c
ext/json/tests/bug72069.phpt

From: Date: Sat, 23 Apr 2016 04:41:44 +0000
Subject: com php-src: Fixed bug #72069 (Behavior \JsonSerializable different from json_encode): NEWS ext/json/json.c ext/json/json _encoder.c
ext/json/tests/bug72069.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-91825@lists.php.net to get a copy of this message
Commit: 589d0e0f187ff82f90110057f624c958f0d3ec86 Author: Xinchen Hui <laruence@gmail.com> Fri, 22 Apr 2016 21:41:44 -0700 Parents: 9eb7c8cd3b202ff77f1442f5a5d3107bd7fc3a18 Branches: PHP-7.0 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=589d0e0f187ff82f90110057f624c958f0d3ec86 Log: Fixed bug #72069 (Behavior \JsonSerializable different from json_encode) Bugs: https://bugs.php.net/72069 Changed paths: M NEWS M ext/json/json.c M ext/json/json_encoder.c A ext/json/tests/bug72069.phpt Diff: diff --git a/NEWS b/NEWS index fc20ad4..64a0743 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,10 @@ PHP NEWS (Nikita) . Fixed bug #72059 (?? is not allowed on constant expressions). (Bob, Marcio) +- JSON: + . Fixed bug #72069 (Behavior \JsonSerializable different from json_encode). + (Laruence) + - OCI8: . Fixed bug #71600 (oci_fetch_all segfaults when selecting more than eight columns). (Tian Yang) diff --git a/ext/json/json.c b/ext/json/json.c index 81258cb..6571bf9 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -253,7 +253,7 @@ static PHP_FUNCTION(json_decode) return; } - JSON_G(error_code) = 0; + JSON_G(error_code) = PHP_JSON_ERROR_NONE; if (!str_len) { JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX; diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index 6c2f377..ae9e086 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -448,6 +448,7 @@ static void php_json_encode_serializable_object(smart_str *buf, zval *val, int o zend_class_entry *ce = Z_OBJCE_P(val); zval retval, fname; HashTable* myht; + int origin_error_code; if (Z_TYPE_P(val) == IS_ARRAY) { myht = Z_ARRVAL_P(val); @@ -461,8 +462,10 @@ static void php_json_encode_serializable_object(smart_str *buf, zval *val, int o return; } + ZVAL_STRING(&fname, "jsonSerialize"); + origin_error_code = JSON_G(error_code); if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL) || Z_TYPE(retval) == IS_UNDEF) { zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ZSTR_VAL(ce->name)); smart_str_appendl(buf, "null", sizeof("null") - 1); @@ -470,6 +473,7 @@ static void php_json_encode_serializable_object(smart_str *buf, zval *val, int o return; } + JSON_G(error_code) = origin_error_code; if (EG(exception)) { /* Error already raised */ zval_ptr_dtor(&retval); diff --git a/ext/json/tests/bug72069.phpt b/ext/json/tests/bug72069.phpt new file mode 100644 index 0000000..0ff8c98 --- /dev/null +++ b/ext/json/tests/bug72069.phpt @@ -0,0 +1,29 @@ +--TEST-- +Bug #72069 (Behavior \JsonSerializable different from json_encode) +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php + +$result = json_encode(['end' => json_decode(null, true)]); +var_dump($result); + +class A implements \JsonSerializable +{ + function jsonSerialize() + { + return ['end' => json_decode(null, true)]; + } +} +$a = new A(); +$toJsonData = $a->jsonSerialize(); +$result = json_encode($a); +var_dump($result); + +$result = json_encode($toJsonData); +var_dump($result); +?> +--EXPECT-- +string(12) "{"end":null}" +string(12) "{"end":null}" +string(12) "{"end":null}"

« previous php.cvs (#91825) next »