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

Jul 12, 2018 am 11:39 AM

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
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)