Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

utf8 encoded error #31

Open
Hoszi opened this issue May 18, 2017 · 1 comment
Open

utf8 encoded error #31

Hoszi opened this issue May 18, 2017 · 1 comment

Comments

@Hoszi
Copy link

Hoszi commented May 18, 2017

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().

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-json; a new issue has been opened at laminas/laminas-json#2.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants