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

utf8 encoded error #31

Open
Open
@Hoszi

Description

@Hoszi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions