From: arnfda at gmail dot com Operating system: OS X 10.6.8 PHP version: 5.4.4 Package: JSON related Bug Type: Feature/Change Request Bug description:Function that always produces legal JSON
Description: ------------ json_encode generates illegal JSON when it is passed a string, boolean, or number. This behavior is noted here, along with a workaround: http://www.php.net/manual/en/function.json-encode.php#92817 json_decode accepts the illegal JSON, but other parsers that are more strict do not (for example, MultiJson in Ruby). I know the documentation doesn't promise to return valid JSON text, just the "JSON representation" of whatever is passed into it. So strictly speaking, this isn't a bug. But it doesn't make sense to have a function that creates JSON fragments and not one that always produces legal JSON that you can safely pass to other systems. It would be great if an option was added to json_encode that forced it to produce legal JSON strings, e.g. by wrapping single values in brackets as in the examples below. Test script: --------------- var_dump(json_encode(1)); var_dump(json_encode(true)); var_dump(json_encode("string")); Expected result: ---------------- string(3) "[1]" string(6) "[true]" string(10) "["string"]" Actual result: -------------- string(1) "1" string(4) "true" string(8) ""string"" -- Edit bug report at https://bugs.php.net/bug.php?id=62422&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=62422&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=62422&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=62422&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=62422&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=62422&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=62422&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=62422&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=62422&r=needscript Try newer version: https://bugs.php.net/fix.php?id=62422&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=62422&r=support Expected behavior: https://bugs.php.net/fix.php?id=62422&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=62422&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=62422&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=62422&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=62422&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=62422&r=dst IIS Stability: https://bugs.php.net/fix.php?id=62422&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=62422&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=62422&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=62422&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=62422&r=mysqlcfg