From:             masakielastic at gmail dot com
Operating system: All
PHP version:      5.5.0
Package:          JSON related
Bug Type:         Feature/Change Request
Bug description:json_encode's option for replacing ill-formd byte sequences 
with substitute cha

Description:
------------
json_encode returns false if the string contains ill-formed byte 
sequences. It is hard to find the problem since a lot of web applications
don't 
expect the existence of ill-formed byte sequences. The one example is
Symfony's 
JsonResponse class.

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundat
ion/JsonResponse.php#L83

Introducing json_encode's option for replacing ill-formd byte sequences
with 
substitute characters (such as U+FFFD) save writing the logic.

function json_encode2($value, $options, $depth)
{
    if (is_scalar($value)) {
        return json_encode($value, $options, $depth);
    }

    $value2 = [];

    foreach ($value as $key => $elm) {

        $value2[str_scrub($key)] = str_scrub($elm);

    }

    return json_encode($value2, $options, $depth);
}


// https://bugs.php.net/bug.php?id=65081
function str_scrub($str, $encoding = 'UTF-8')
{
    return htmlspecialchars_decode(htmlspecialchars($str, ENT_SUBSTITUTE, 
$encoding));
}

The precedent example is htmlspecialchars's ENT_SUBSTITUTE option which was

introduced 
in PHP 5.4. json_encode shares the part of logic used such as
php_next_utf8_char 
by htmlspecialchars since PHP 5.5.

https://github.com/php/php-src/blob/master/ext/json/json.c#L369

Another reason for introducing the option is existence of JsonSerializable

interface.

Accessing jsonSerialize method's values come from private properties is
hard 
or impossbile.

The one of names of candiates for the option is JSON_SUBSTITUTE similar to

htmlspecialchar's ENT_SUBSTITUTE option.

json_encode($object, JSON_SUBSTITUTE);


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

Reply via email to