Bug #66025 [Asn]: Indent wrong when json_encode() called from jsonSerialize function

From: Date: Sun, 30 Oct 2016 13:29:05 +0000
Subject: Bug #66025 [Asn]: Indent wrong when json_encode() called from jsonSerialize function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-205069@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66025&edit=1

 ID:                 66025
 Updated by:         bukka@php.net
 Reported by:        pleasestand at live dot com
 Summary:            Indent wrong when json_encode() called from
                     jsonSerialize function
 Status:             Assigned
 Type:               Bug
 Package:            JSON related
 PHP Version:        master-Git-2013-11-03 (Git)
 Assigned To:        bukka
 Block user comment: N
 Private report:     N

 New Comment:

Fixed by http://git.php.net/?p=php-src.git;a=commit;h=c34de0b61c68bd77e324b869760a53abead4c5ed


Previous Comments:
------------------------------------------------------------------------
[2016-05-15 14:53:21] bukka@php.net

The bug is caused by the fact that globals are used for encoder depth. I plan to introduce a
separate context for each encoder run which will fix this and couple of others bugs related to
globals in json encoding. This won't happen before 7.1

------------------------------------------------------------------------
[2015-02-13 01:45:28] encryptio at gmail dot com

I'm hitting this in production at my workplace; specifically, when json_encode causes the
interpereter to run out of memory and abort requests in mod_php, php_json_encode_array doesn't
have a chance to decrement the encoder_depth variable, leaving a thread-local inconsistency in the
interpereter.

------------------------------------------------------------------------
[2013-11-03 13:19:54] pleasestand at live dot com

Description:
------------
This is because the current indent is stored in a variable JSON_G(encoder_depth), which is shared
between json_encode() calls.

Test script:
---------------
<?php

class Foo implements JsonSerializable {
    public function jsonSerialize() {
        return json_encode([1], JSON_PRETTY_PRINT);
    }
}

echo json_encode([new Foo]), "\n";

Expected result:
----------------
["[\n    1\n]"]

Actual result:
--------------
["[\n        1\n    ]"]


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=66025&edit=1


Thread (5 messages)

« previous php.bugs (#205069) next »