zend-json icon indicating copy to clipboard operation
zend-json copied to clipboard

utf8 encoded error

Open Hoszi opened this issue 8 years ago • 1 comments

there is a problem when json_encode get an incorrect character but i have a solution: in Json.php

private static function encodeViaPhpBuiltIn($valueToEncode, $prettyPrint = false)
{
    if (! function_exists('json_encode') || static::$useBuiltinEncoderDecoder === true) {
        return false;
    }

    $encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;

    if ($prettyPrint) {
        $encodeOptions |= JSON_PRETTY_PRINT;
    }
    array_walk_recursive($valueToEncode, function(&$item, $key){
        if(!mb_detect_encoding($item, 'utf-8', true)){
            $item = utf8_encode($item);
        }
    });
    $encoded = json_encode($valueToEncode, $encodeOptions | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    return $encoded ? $encoded : json_last_error_msg();
}

The new section is from array_walk_recursive().

Hoszi avatar May 18 '17 12:05 Hoszi

This repository has been closed and moved to laminas/laminas-json; a new issue has been opened at https://github.com/laminas/laminas-json/issues/2.

weierophinney avatar Dec 31 '19 21:12 weierophinney