Bug #68288 [NEW]: Encoding a clsoure as "{}" in json_encode

From: Date: Wed, 22 Oct 2014 23:30:29 +0000
Subject: Bug #68288 [NEW]: Encoding a clsoure as "{}" in json_encode
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188269@lists.php.net to get a copy of this message
From: dmitry dot soshnikov at gmail dot com Operating system: CentOS 6 PHP version: 5.5Git-2014-10-22 (Git) Package: JSON related Bug Type: Bug Bug description:Encoding a clsoure as "{}" in json_encode Description: ------------ The json_encode in PHP 5.5.17 encodes a closure object as "{}". By analyzing the code, I found out that it might be a bug, since json encoder doesn't have any special treatment of closure objects, and just fallback to the array-encoder, which eventually outputs the "{}". Example: // PHP json_encode([1,2,3, function () {}]); // "[1,2,3,{}]" In HHVM team we'd like to conform the behavior (the discussion is in this issue thread: https://github.com/facebook/hhvm/issues/4035), however, think that encoding a closure as "{}" doesn't make big sense, since it cannot be decoded back as a closure. For the record, e.g. ECMAScript encodes all non-JSON values as undefined (or null in arrays): // ECMAScript JSON.stringify([1,2,3, function () {}]) // "[1,2,3,null]" My question are: 1. Is it actual PHP bug? 2. If yes, will it make sense to encode closure objects as null instead of "{}"? Dmitry Test script: --------------- <?php json_encode([1,2,3, function () {}]); // "[1,2,3,{}]" Expected result: ---------------- "[1,2,3,null]" Actual result: -------------- "[1,2,3,{}]" -- Edit bug report at https://bugs.php.net/bug.php?id=68288&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68288&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68288&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68288&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=68288&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=68288&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=68288&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=68288&r=needscript Try newer version: https://bugs.php.net/fix.php?id=68288&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=68288&r=support Expected behavior: https://bugs.php.net/fix.php?id=68288&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=68288&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=68288&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=68288&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68288&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=68288&r=dst IIS Stability: https://bugs.php.net/fix.php?id=68288&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=68288&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=68288&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=68288&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=68288&r=mysqlcfg

« previous php.bugs (#188269) next »