Bug #71835 [Ana]: json_encode sometimes incorrectly detects recursion with JsonSerializable
Edit report at https://bugs.php.net/bug.php?id=71835&edit=1
ID: 71835
Updated by: laruence@php.net
Reported by: icarpenter at leadid dot com
Summary: json_encode sometimes incorrectly detects recursion
with JsonSerializable
Status: Analyzed
Type: Bug
Package: JSON related
PHP Version: 7.0.4
-Assigned To:
+Assigned To: bukka
Block user comment: N
Private report: N
New Comment:
Hmm, I can not think out of a good fix for this, @bukka, what do you think?
Previous Comments:
------------------------------------------------------------------------
[2016-03-16 02:59:32] laruence@php.net
hmm, the problem is get_object_vars maybe return directly object->properties, which make it looks
like a recursive array ,
------------------------------------------------------------------------
[2016-03-16 02:00:51] icarpenter at leadid dot com
Description:
------------
If a class that implements JsonSerializable returns an array of get_object_vars($this) in its
jsonSerialize method, and if that class is in an array that is json_encode'd,
JSON_ERROR_RECURSION is returned.
This does not seem to happen if jsonSerialize returns get_object_vars($this) not enclosed an array,
nor does it seem to happen if the object itself is json_encode'd
Test script:
---------------
https://3v4l.org/OoR7D
Expected result:
----------------
string(4) "[[]]"
bool(false)
string(6) "[[[]]]"
bool(false)
string(2) "[]"
bool(false)
string(4) "[[]]"
bool(false)
Actual result:
--------------
string(4) "[[]]"
bool(false)
bool(false)
bool(true)
string(2) "[]"
bool(false)
string(4) "[[]]"
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71835&edit=1
Thread (6 messages)