CKTIS / PHP
JsonException
Capture les erreurs des opérations JSON avec JSON_THROW_ON_ERROR.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
catch (JsonException $error) { /* inspect JSON error */ }Capture les erreurs des opérations JSON avec JSON_THROW_ON_ERROR. Le code indique la nature de l’erreur, par exemple JSON_ERROR_SYNTAX. Le JSON null valide se décode normalement et se distingue d’une entrée mal formée. Les options par défaut n’activent pas l’exception.
try { json_decode("{",true,512,JSON_THROW_ON_ERROR); }
catch(JsonException $e){$syntax=$e->getCode()===JSON_ERROR_SYNTAX;}
echo json_encode([$syntax,json_decode("null",true,512,JSON_THROW_ON_ERROR)]);
[true,null]
Exemples
<?php
try { json_decode("{",true,512,JSON_THROW_ON_ERROR); }
catch(JsonException $e){$syntax=$e->getCode()===JSON_ERROR_SYNTAX;}
echo json_encode([$syntax,json_decode("null",true,512,JSON_THROW_ON_ERROR)]);
Résultat
[true,null]