Bug #73223 [NEW]: json_encode() returns false when depth is 1 and a complex object is provided

From: Date: Sun, 02 Oct 2016 05:06:20 +0000
Subject: Bug #73223 [NEW]: json_encode() returns false when depth is 1 and a complex object is provided
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204397@lists.php.net to get a copy of this message
From:             jimmymasaru at gmail dot com
Operating system: macOS
PHP version:      5.6.26
Package:          *General Issues
Bug Type:         Bug
Bug description:json_encode() returns false when depth is 1 and a complex object is provided

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 bug report at https://bugs.php.net/bug.php?id=73223&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=73223&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=73223&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=73223&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=73223&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=73223&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=73223&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=73223&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=73223&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=73223&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=73223&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=73223&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=73223&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=73223&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73223&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=73223&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=73223&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=73223&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73223&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=73223&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=73223&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#204397) next »