PHP Classes
elePHPant
Icontem

PHP JSON: Load and save variable values to JSON files

Recommend this page to a friend!
  Info   View files Documentation   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-04-08 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 183 This week: 1All time: 8,271 This week: 468Up
Version License PHP version Categories
php-json 1.1.7MIT/X Consortium ...5.6PHP 5, Files and Folders, Data types
Description Author

This class can load and save variable values to JSON files.

It can create one file and save a given variable in JSON encoded format. It can create the directory of the file if it does not exist.

The class can also load a variable value from a local or remote file with the value encoded in JSON format.

  Performance   Level  
Name: Josantonius <contact>
Classes: 30 packages by
Country: Spain Spain
Innovation award
Innovation award
Nominee: 11x

Details

PHP Json library

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

PHP simple library for managing Json files.

Requirements

This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.

Installation

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

Or install it manually:

Download Json.php, JsonLastError.php and JsonException.php:

$ wget https://raw.githubusercontent.com/Josantonius/PHP-Json/master/src/Json.php
$ wget https://raw.githubusercontent.com/Josantonius/PHP-Json/master/src/JsonLastError.php
$ wget https://raw.githubusercontent.com/Josantonius/PHP-Json/master/src/Exception/JsonException.php

Available Methods

Available methods in this library:

- Creating JSON file from array:

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)

- Save to array the JSON file content:

Json::fileToArray($file);

| Attribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $file | Path or external url to JSON file. | string | Yes | |

# Return (array|false)

- Check for errors:

JsonLastError::check();

# Return (array|null) ? Null if there are no errors or array with state code and error message.

- Get collection of JSON errors:

JsonLastError::getCollection();

# Return (array) ? Collection of JSON errors.

Quick Start

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;

Usage

Example of use for this library:

- Creating JSON file from array:


$array = [
	'name'  => 'Josantonius',
    'email' => 'info@josantonius.com',
    'url'   => 'https://github.com/josantonius/PHP-Json'
];

$pathfile = __DIR__ . '/filename.json';

Json::arrayToFile($array, $pathfile);

- Save to array the JSON file content:

$pathfile = __DIR__ . '/filename.json';

$array = Json::fileToArray($pathfile);

- Check for errors:

$lastError = JsonLastError::check();

if (!is_null($lastError)) {
    var_dump($lastError);
}

- Get collection of JSON errors:

$jsonLastErrorCollection = JsonLastError::getCollection();

Tests

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

Exception Handler

This library uses exception handler that you can customize.

? TODO

  • [ ] Add new feature.
  • [ ] Improve tests.
  • [ ] Improve documentation.
  • [ ] Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.

Contribute

If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command composer install to install the dependencies. This will also install the dev dependencies.
  • Run the command composer fix to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.

Thank you to all the people who already contributed to this project!

<img alt="peter279k" src="https://avatars2.githubusercontent.com/u/9021747?v=4&s=117" height="117" width="117"> | :---:| peter279k|

Repository

The file structure from this repository was created with PHP-Skeleton.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2016 - 2018 Josantonius, josantonius.com

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (2 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpmd.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README-ES.md Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageException (1 file)
  Plain text file Json.php Class Class source
  Plain text file JsonLastError.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file JsonException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file JsonLastErrorTest.php Class Class source
  Plain text file JsonTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:183
This week:1
All time:8,271
This week:468Up