Skip to content

josantonius/php-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP JSON library

Latest Stable Version License Total Downloads CI PSR2 PSR4 CodeCov

Translations: Español

PHP simple library for managing JSON files.



Requirements

This library is compatible with the PHP versions: 5.6 | 7.0 | 7.1 | 7.2 | 7.3 | 7.4.

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:

Create 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:

Create a JSON file from an array

$array = [
 'name'  => 'josantonius',
    'email' => 'info@josantonius.dev',
    'url'   => 'https://github.com/josantonius/php-json'
];

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

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

Save the contents of the JSON file in an array

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

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

Checks for errors

$lastError = JsonLastError::check();

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

Get a JSON error collection

$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.
  • Improve English translation in the README file.
  • Refactor code for disabled code style rules. See phpmd.xml and phpcs.xml.

Changelog

Detailed changes for each release are documented in the release notes.

Contribution

Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.

Thanks to all contributors! ❤️

Sponsor

If this project help you reduce time to develop, you can become my sponsor! 😊

License

This repository is licensed under the MIT License.

Copyright © 2016-present, Josantonius

About

PHP simple library for managing JSON files

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors

Languages