Bug #73223 [Opn->Nab]: json_encode() returns false when depth is 1 and a complex object is provided
Edit report at https://bugs.php.net/bug.php?id=73223&edit=1
ID: 73223
Updated by: requinix@php.net
Reported by: jimmymasaru at gmail dot com
Summary: json_encode() returns false when depth is 1 and a
complex object is provided
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: macOS
PHP Version: 5.6.26
Block user comment: N
Private report: N
New Comment:
It has always returned false. https://3v4l.org/AHvH6
As the error indicates, the problem is the depth limit: encoding reaches that limit and fails. The
behavior you're seeing now is correct - it shouldn't serialize data if it cannot
completely serialize the data.
If you want a method to "trim complex objects" then I would suggest get_object_vars() +
filtering to remove/substitute object values + json_encode() on the resulting array.
You're getting false now and were not before? Then that means something changed besides the
version of PHP. What is it? Are you using the bundled JSON implementation or one provided externally
(eg, jsonc)?
Previous Comments:
------------------------------------------------------------------------
[2016-10-02 05:06:18] jimmymasaru at gmail dot com
Description:
------------
I use json_encode() with a depth (usually 1) to trim complex objects in some test cases in order to
do comparisons.
But it returns false now if the object being encoded contains a member which is an object.
I have to set the depth to 2 but it means it doesn't trim like what I did before.
But I'm not sure if this bug is related to PHP 5.6.26. It might have been introduced in earlier
versions.
Test script:
---------------
class Person
{
public $name;
public $age;
public $role;
}
class Role
{
public $name;
}
$person = new Person();
$person->name = 'Name';
$person->age = 20;
$person->role = new Role();
$jsonObj = json_encode($person, 0, 1);
// $jsonObj === false
Expected result:
----------------
{"name":"Name","age":20,"role":null}
Actual result:
--------------
false
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73223&edit=1
Thread (2 messages)