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

From: Date: Thu, 18 Dec 2014 18:24:25 +0000
Subject: Bug #68288 [Com]: Encoding a clsoure as "{}" in json_encode
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189112@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68288&edit=1 ID: 68288 Comment by: dthomas at starkartthenala dot com Reported by: dmitry dot soshnikov at gmail dot com Summary: Encoding a clsoure as "{}" in json_encode Status: Open Type: Bug Package: JSON related Operating System: CentOS 6 PHP Version: 5.5Git-2014-10-22 (Git) Block user comment: N Private report: N New Comment: FWIW, I'm experiencing this with Ubuntu 14.04, PHP 5.5.9-1ubuntu4.5 (cli) (built: Oct 29 2014 11:59:10) Previous Comments: ------------------------------------------------------------------------ [2014-10-22 23:30:29] dmitry dot soshnikov at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=68288&edit=1

« previous php.bugs (#189112) next »