Bug #63898 [Opn]: json_encode sets string to null for invalid characters

From: Date: Fri, 04 Jan 2013 01:06:40 +0000
Subject: Bug #63898 [Opn]: json_encode sets string to null for invalid characters
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-175727@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63898&edit=1

 ID:                 63898
 User updated by:    sreed at ontraport dot com
 Reported by:        sreed at ontraport dot com
 Summary:            json_encode sets string to null for invalid
                     characters
 Status:             Open
 Type:               Bug
 Package:            JSON related
 Operating System:   All
 PHP Version:        5.4.10
 Block user comment: N
 Private report:     N

 New Comment:

.


Previous Comments:
------------------------------------------------------------------------
[2013-01-04 01:04:31] sreed at ontraport dot com

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 this bug report at https://bugs.php.net/bug.php?id=63898&edit=1


Thread (9 messages)

« previous php.bugs (#175727) next »