Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
joanhey opened this issue Dec 9, 2023 · 0 comments · Fixed by #374
Closed

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

joanhey opened this issue Dec 9, 2023 · 0 comments · Fixed by #374

Comments

@joanhey
Copy link
Contributor

joanhey commented Dec 9, 2023

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

joanhey added a commit to joanhey/php-ddd-example that referenced this issue Dec 9, 2023
Ahora es más rápido.

Lo curioso es que si lo usan el jsonEncode().

Fix: CodelyTV#373
JavierCane added a commit that referenced this issue Dec 11, 2023
* Use json_decode exception

Ahora es más rápido.

Lo curioso es que si lo usan el jsonEncode().

Fix: #373

* fix: remove unused import

---------

Co-authored-by: Javier Ferrer González <javier.ferrer@codely.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant