CKTIS / PHP
JsonException
Captura erros das operações JSON com JSON_THROW_ON_ERROR.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
catch (JsonException $error) { /* inspect JSON error */ }Captura erros das operações JSON com JSON_THROW_ON_ERROR. O código identifica a falha, por exemplo JSON_ERROR_SYNTAX. O JSON null válido descodifica-se normalmente e distingue-se de dados malformados. As opções predefinidas não ativam a exceção.
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]
Exemplos
<?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)]);
Resultado
[true,null]