From:             sreed at sendpepper dot com
Operating system: All
PHP version:      5.4.10
Package:          JSON related
Bug Type:         Bug
Bug description:json_encode sets string to null for invalid characters

Description:
------------
When you use json_encode with an invalid UTF-8 byte sequence in a string
PHP will 
generate a warning (with display_errors set to off) and the function
returns an 
invalid json encoded string. The string with the invalid UTF-8 byte
sequence is 
replaced with null (for example: {null:""}). This is invalid json and can
not be 
decoded with json_decode.

I would think the expected behavior should be that json_encode should never

returns an invalid json encoded string. It should either return false on
failure 
as the documentation states or the invalid UTF-8 byte sequence should be
handled 
in a way that does not corrupt the json string.

Test script:
---------------
$key = "Foo " . chr(163);

$array = array($key => "");

var_dump($array);

$json = json_encode($array);

echo $json."\n";

var_dump(json_decode($json));

Expected result:
----------------
I would expect the returned json string to be valid or for json_encode to
return 
false. 

Actual result:
--------------
array(1) {
  ["Foo �"]=>
  string(0) ""
}
{null:""}
NULL


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

Reply via email to