Skip to content

deprecate json_encode() of non-serializable instances #15724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/json/json_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,11 @@ zend_result php_json_encode_zval(smart_str *buf, zval *val, int options, php_jso
if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) {
return php_json_encode_serializable_enum(buf, val, options, encoder);
}
if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_NOT_SERIALIZABLE) {
if (!(Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ANON_CLASS) || (Z_OBJCE_P(val)->parent && Z_OBJCE_P(val)->parent->ce_flags & ZEND_ACC_NOT_SERIALIZABLE)) {
zend_error(E_DEPRECATED, "json_encode() of non-serializable class %s is deprecated", ZSTR_VAL(Z_OBJCE_P(val)->name));
}
}
/* fallthrough -- Non-serializable object */
ZEND_FALLTHROUGH;
case IS_ARRAY: {
Expand Down
24 changes: 24 additions & 0 deletions tests/classes/json_encode_non_serializable_deprecated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
json_encode() on instances of classes market with ZEND_ACC_NOT_SERIALIZABLE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be located in the corresponding tests directory for json extension, ext/json/tests. And despite the outcome of the discussion on the internal list, I would see the benefit of including the tests now. I was surprised that those paths were not tested already.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor typo

Suggested change
json_encode() on instances of classes market with ZEND_ACC_NOT_SERIALIZABLE
json_encode() on instances of classes marked with ZEND_ACC_NOT_SERIALIZABLE

--FILE--
<?php
function a() { for ($i=0; $i < 10; $i++) { yield $i; }};
$fn = a(...);

$c1 = new class() extends PDOStatement {};
$c2 = new class() extends stdClass {};
$c3 = new class() {};

echo json_encode(a());
echo json_encode($fn);
echo json_encode($c1);
echo json_encode($c2);
echo json_encode($c3);
?>
--EXPECTF--
Deprecated: json_encode() of non-serializable class Generator is deprecated in %s on line %d
{}
Deprecated: json_encode() of non-serializable class Closure is deprecated in %s on line %d
{}
Deprecated: json_encode() of non-serializable class PDOStatement@anonymous is deprecated in %s on line %d
{}{}{}
Loading