Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
deprecate json_encode() of non-serializable instances
there's a good reason for classes being marked as non-serializable, so it follows that the same should probably be true for json_encoding them as well. As this flag is only available to internal classes, no userland code should be affected.
  • Loading branch information
pilif committed Sep 3, 2024
commit 3a607a44b27bf794f84daa75c4ff9c6990e4caf6
3 changes: 3 additions & 0 deletions ext/json/json_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ 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) {
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
15 changes: 15 additions & 0 deletions tests/classes/json_encode_non_serializable_deprecated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--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(...);

echo json_encode(a());
echo json_encode($fn);
?>
--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
{}