Home > headlines > Take a look at JSON error handling in the new version of PHP 7.3

Take a look at JSON error handling in the new version of PHP 7.3

无忌哥哥
Release: 2018-07-12 11:39:34
Original
3674 people have browsed it

Background

In the currently stable PHP V7.2, if you want to determine that the JSON is invalid, you must use the json_last_error() function verification:

>>> json_decode("{");
=> null
>>> json_last_error();
=> 4
>>> json_last_error() === JSON_ERROR_NONE
=> false
>>> json_last_error_msg()
=> "Syntax error"
Copy after login

For example, here's a check in Larave to make sure calling JSON encoding doesn't result in an error:

// Once we get the encrypted value we'll go ahead and base64_encode the input
// vector and create the MAC for the encrypted value so we can then verify
// its authenticity. Then, we'll JSON the data into the "payload" array.
$json = json_encode(compact('iv', 'value', 'mac'));
if (json_last_error() !== JSON_ERROR_NONE) {
    throw new EncryptException('Could not encrypt the data.');
}
return base64_encode($json);
Copy after login

We can at least determine if there is an error in JSON encoding/decoding, but that's a bit clunky compared to throwing An exception emits an error code and error message.

While you already have options for capturing and processing JSON, let's see what the new version can do for us in a nice way!

Throwing Error Flags in PHP 7.3

With the new option flag JSON_THROW_ON_ERROR it is possible to rewrite this block of code to use try/catch.

Maybe something like the following:

use JsonException;
try {
    $json = json_encode(compact('iv', 'value', 'mac'), JSON_THROW_ON_ERROR);
    return base64_encode($json);
} catch (JsonException $e) {
    throw new EncryptException('Could not encrypt the data.', 0, $e);
}
Copy after login

I think this new style is especially useful for user code when you receive some JSON data instead of searching for json_last_error() and matching options to JSON Encoding and decoding can take advantage of error handlers.

This json_decode() function takes a few parameters and will look like PHP 7.3 or below if you want to take advantage of error handling:

use JsonException;
try {
    return json_decode($jsonString, $assoc = true, $depth = 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
    // Handle the JSON Exception
}
// Or even just let it bubble up...
/** 
 * Decode a JSON string into an array
 *
 * @return array
 * @throws JsonException
 */
function decode($jsonString) {
    return json_decode($jsonString, $assoc = true, $depth = 512, JSON_THROW_ON_ERROR);
}
Copy after login

Get the error code and error message

Previously you used the following function to get the JSON error code and message:

// Error code
json_last_error();
// Human-friendly message
json_last_error_msg();
Copy after login

If you use the new JSON_THROW_ON_ERROR, here is how you use the code and get the message:

try {
    return json_decode($jsonString, $assoc = true, $depth = 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
    $e->getMessage(); // like json_last_error_msg()
    $e->getCode(); // like json_last_error()
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template