seld/jsonlint
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The seld/jsonlint library is a comprehensive tool for working with JSON data, more specifically transforming and validating it. It is designed to parse JSON while providing meaningful error messages upon encountering invalid JSON data, which enhances debugging capabilities significantly. Please note, this library might be slower than the native PHP function json_decode()
due to its added functionality and improvements in error handling.
To begin using seld/jsonlint in your PHP project, you can include it via Composer with the command composer require seld/jsonlint
. Initialise a new instance of the JsonParser class, then call appropriate methods on this object as per your need.
Here is a simple example of using the library to parse a JSON string.
use Seld\JsonLint\JsonParser;
$parser = new JsonParser();
try {
// Parsing JSON string
$data = $parser->parse($jsonString);
} catch (ParsingException $e) {
echo 'Parse error: ', $e->getMessage(), "\n", 'Details: ', $e->getDetails();
}
You can also handle duplicate keys in JSON strings like so:
use Seld\JsonLint\JsonParser;
$parser = new JsonParser();
try {
$data = $parser->parse($jsonString, JsonParser::DETECT_KEY_CONFLICTS);
} catch (DuplicateKeyException $e) {
$details = $e->getDetails();
echo 'Key '.$details['key'].' is a duplicate at line '.$details['line'];
}
Remember, you can pass flags to JsonParser::lint/parse
to alter its behaviour. For instance, JsonParser::DETECT_KEY_CONFLICTS
throws an exception on encountering duplicate keys.
The documentation for seld/jsonlint is available in its GitHub repository (https://github.com/Seldaek/jsonlint). The readme file contains all the necessary guidelines and examples for using the library effectively. It provides an exhaustive list of methods provided by the library, along with their intended usage and examples. For further details on a specific topic or issue, refer to the issues section of the repository.