Skip to content

json_encode() y json_decode() puede lanzar exception desde PHP 7.3 #373

Closed
@joanhey

Description

@joanhey

Info:
https://www.php.net/manual/en/class.jsonexception
https://wiki.php.net/rfc/json_throw_on_error

Este código sobra o se puede reducir a 1 línea.

public static function jsonDecode(string $json): array
{
$data = json_decode($json, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new RuntimeException('Unable to parse response body into JSON: ' . json_last_error());
}
return $data;

Lo vi en vuestro video https://youtu.be/hXCT1jZt3vQ?t=3606
No es necesario esperar a json_decode2().

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