在「PHP: rfc:json_throw_on_error」這邊提到 PHP 7.3 會解決 json_decode()
發生錯誤時的處理方式:
PHP has two functions for dealing with JSON,
json_decode()
andjson_encode()
. Unfortunately, both have suboptimal error handling.json_decode()
returnsnull
upon erroring, butnull
is also a possible valid result (if decoding the JSON “null”).
在這之前唯一的判斷方式是另外再呼叫 json_last_error()
或是 json_last_error_msg()
,但這樣寫很辛苦,所以要引入 JsonException
了,總算...