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 (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-12-06 (9 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 148 This week: 7All time: 8,462 This week: 97Up
Version License PHP version Categories
php-json 1.1.6MIT/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 is available for providing paid consulting. Contact Josantonius .
Classes: 19 packages by
Country: Spain Spain
Age: ???
All time rank: 226355 in Spain Spain
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 8x

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

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__ . '/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);

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

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.

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 - 2017 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 (1 file, 1 directory)
Files folder imagetests (1 file)
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

  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 JsonTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:148
This week:7
All time:8,462
This week:97Up