Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Zend\Json\Json::decode null or empty string throw Zend\Json\Exception\RuntimeException on PHP7 #6

Closed
weierophinney opened this issue Dec 31, 2019 · 5 comments

Comments

@weierophinney
Copy link
Member

Similar to zendframework/zf1#678. ZF1 fix PR on zendframework/zf1#680.


Originally posted by @yuripave at zendframework/zend-json#23

@weierophinney
Copy link
Member Author

As already told in zendframework/zend-cache#95


Originally posted by @marc-mabe at zendframework/zend-json#23 (comment)

@weierophinney
Copy link
Member Author

Agree as a serializer component we should throw an exception in case of trying to unserialize or decode invalid input.
Those that upgrading PHP5 to PHP7 will need to deal with json_last_error behavior change such that json_decode('') PHP5 return JSON_ERROR_NONE but PHP7 return JSON_ERROR_SYNTAX.

References:
https://bugs.php.net/bug.php?id=71718
http://php.net/manual/en/function.json-last-error.php#118165
http://stackoverflow.com/questions/36362856/php7-json-and-zend-decoding-failed-syntax-error


Originally posted by @yuripave at zendframework/zend-json#23 (comment)

@weierophinney
Copy link
Member Author

Php changed behavior of json_last_error with major version changing. Component zend-json doesn't change major or even minor version. In my opinion, it must preserve backward compatibility and fix issue on php7. Anyway, component should have the same behaviour on all supported versions of php.


Originally posted by @v-noskov at zendframework/zend-json#23 (comment)

@weierophinney
Copy link
Member Author

This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Steering Committee.
If you have a security issue, please follow our security reporting guidelines.
If you wish to take on the role of maintainer, please nominate yourself

@cline-pk
Copy link

cline-pk commented May 20, 2023

function utf8_json_encode($array, $key = null)
{
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = utf8_json_encode($value, $key); //recursive
        }
    } elseif (is_string($array)) {
        $fixed = mb_convert_encoding($array, "UTF-8", "UTF-8");
        return $fixed;
    }
    return json_encode($array);
	
}

function utf8_json_decode($json) {
	
	$res = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json), true);
	return $res;
	
}


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