Translations: Español
PHP simple library for managing JSON files.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- TODO
- Changelog
- Contribution
- Sponsor
- License
This library is compatible from PHP 8.0 version to PHP 8.1 version.
To continue using the version with static methods without the new features:
-
For older versions of PHP (from 5.6 to 7.4), version 1.1.9 of this library can be used.
-
For PHP versions 8.0 and 8.1, version 1.2.0 of this library can be used.
The preferred way to install this extension is through Composer.
To install PHP JSON library, simply:
composer require josantonius/json
The 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-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-json.git
Available methods in this library:
$json->get();
@throws CreateDirectoryException | CreateFileException | JsonErrorException
@Return array
- File contents
$json->set(array|object $content);
@throws CreateFileException | JsonErrorException | UnavailableMethodException
@Return void
$json->merge(array|object $content);
@throws CreateFileException | GetFileException | JsonErrorException | UnavailableMethodException
@Return array
- Resulting array
$json->push(array|object $content);
@throws CreateFileException | GetFileException | JsonErrorException | UnavailableMethodException
@Return array
- Resulting array
To use this library with Composer:
require __DIR__ . '/vendor/autoload.php'; use josantonius\Json\Json;
$json = new Json('path/to/file.json'); # If the file does not exist it will be created.
OR
$json = new Json('https://site.com/file.json'); # When the JSON file is obtained from a URL, only the "get" method is available.
Example of use for this library:
{ "foo": "bar" }
$json->get();
['foo' => 'bar']
$json->set(['foo' => 'bar']);
{ "foo": "bar" }
{ "foo": "bar" }
$json->merge(['bar' => 'foo']);
{ "foo": "bar", "bar": "foo" }
[ { "name": "foo" } ]
$json->push(['name' => 'bar']);
[ { "name": "foo" }, { "name": "bar" } ]
To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-json.git
cd php-json
composer install
Run unit tests with PHPUnit:
composer phpunit
Run PSR2 code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
- 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 helps you to reduce your development time and you want to thank me, you could sponsor me! 😊
This repository is licensed under the MIT License.
Copyright © 2016-present, Josantonius