Translations: Español
PHP simple library for managing JSON files.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- Exception Handler
- TODO
- Changelog
- Contribution
- Sponsor
- License
This library is compatible from PHP 8.0 version to PHP 8.1 version.
For older versions of PHP (from 5.6 to 7.4) you can use version 1.1.9 of this library.
The preferred way to install this extension is through Composer.
To install PHP JSON library, simply:
composer require josantonius/jsonThe previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require josantonius/json --prefer-sourceYou can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-json.gitOr install it manually:
Download Json.php, JsonLastError.php and JsonException.php:
wget https://raw.githubusercontent.com/josantonius/php-json/main/src/Json.phpwget https://raw.githubusercontent.com/josantonius/php-json/main/src/JsonLastError.phpwget https://raw.githubusercontent.com/josantonius/php-json/main/src/Exception/JsonException.phpAvailable methods in this library:
Json::arrayToFile($array, $file);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $array | Array to be converted to JSON. | array | Yes | |
| $file | Path to the file. | string | Yes |
# Return (boolean)
Json::fileToArray($file);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $file | Path or external url to JSON file. | string | Yes |
# Return (array|false)
JsonLastError::check();# Return (array|null) → Null if there are no errors or array with state code and error message.
JsonLastError::getCollection();# Return (array) → Collection of JSON errors.
To use this library with Composer:
require __DIR__ . '/vendor/autoload.php';
use josantonius\Json\Json;Or If you installed it manually, use it:
require_once __DIR__ . '/Json.php';
require_once __DIR__ . '/JsonLastError.php';
require_once __DIR__ . '/JsonException.php';
use josantonius\Json\Json;Example of use for this library:
$array = [
'name' => 'josantonius',
'email' => 'info@josantonius.dev',
'url' => 'https://github.com/josantonius/php-json'
];
$filepath = __DIR__ . '/filename.json';
Json::arrayToFile($array, $filepath);$filepath = __DIR__ . '/filename.json';
$array = Json::fileToArray($filepath);$lastError = JsonLastError::check();
if (!is_null($lastError)) {
var_dump($lastError);
}$jsonLastErrorCollection = JsonLastError::getCollection();To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-json.gitcd php-jsoncomposer installRun unit tests with PHPUnit:
composer phpunitRun PSR2 code standard tests with PHPCS:
composer phpcsRun PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmdRun all previous tests:
composer testsThis library uses exception handler that you can customize.
- Add new feature.
- Improve tests.
- Improve documentation.
- Improve English translation in the README file.
- Refactor code for disabled code style rules. See phpmd.xml and phpcs.xml.
Detailed changes for each release are documented in the release notes.
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
If this project help you reduce time to develop, you can become my sponsor! 😊
This repository is licensed under the MIT License.
Copyright © 2016-present, Josantonius