mszewcz/php-json-utils

JSON utility class

1.0.0 2017-11-01 22:42 UTC

This package is auto-updated.

Last update: 2024-04-16 08:14:23 UTC


README

JSON utility class, which provides encoding, decoding, base64url encoding and base64url decoding methods.

Build Status Codacy Badge Codacy Badge

Contents

Installation

If you use Composer to manage the dependencies simply add a dependency on mszewcz/php-json-utils to your project's composer.json file. Here is a minimal example of a composer.json:

{
    "require": {
        "mszewcz/php-json-utils": ">=1.0"
    }
}

You can also clone or download this respository.

php-json-utils meets PSR-4 autoloading standards. If using the Composer please include its autoloader file:

require_once 'vendor/autoload.php';

If you cloned or downloaded this repository, you will have to code your own PSR-4 style autoloader implementation.

Usage

To encode data into JSON string:

use MS\Json\Utils\Utils;

$obj = new \stdClass();
$obj->flt = 5.5;
$obj->bool = true;

$data = ['int' => 8, 'str' => 'test/Ʃ', 'arr' => [$obj]];

$utils = new Utils();
try {
    $encoded = $utils->encode($data);
} catch (\Exception $e) {
    echo $e->getMessage();
}   

To decode JSON string:

use MS\Json\Utils\Utils;

$json = '{"int":8,"str":"test/Ʃ","arr":[{"flt":5.5,"bool":true}]}';

$utils = new Utils();
try {
    $decoded = $utils->decode($json);
} catch (\Exception $e) {
    echo $e->getMessage();
}   
*/

To base64url encode string:

use MS\Json\Utils\Utils;

$data = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eget purus ';
$data.= 'consectetur eros pellentesque ullamcorper. Sed justo tellus, porttitor non ';
$data.= 'porta ac, euismod eu mauris. Nam maximus pretium dapibus. Pellentesque in elit ';
$data.= 'placerat, sagittis justo id, elementum elit. Maecenas dignissim dui ac lectus ';
$data.= 'pretium condimentum. Morbi id ipsum in urna egestas varius in vitae quam. ';
$data.= 'Phasellus efficitur elementum sapien id dictum.';

$utils = new Utils();
$encoded = $utils->base64UrlEncode($data);

To base64url decode string:

use MS\Json\Utils\Utils;

$data = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4';
$data.= 'gTnVuYyBlZ2V0IHB1cnVzIGNvbnNlY3RldHVyIGVyb3MgcGVsbGVudGVzcXVlIHVsbGFtY29ycG';
$data.= 'VyLiBTZWQganVzdG8gdGVsbHVzLCBwb3J0dGl0b3Igbm9uIHBvcnRhIGFjLCBldWlzbW9kIGV1I';
$data.= 'G1hdXJpcy4gTmFtIG1heGltdXMgcHJldGl1bSBkYXBpYnVzLiBQZWxsZW50ZXNxdWUgaW4gZWxp';
$data.= 'dCBwbGFjZXJhdCwgc2FnaXR0aXMganVzdG8gaWQsIGVsZW1lbnR1bSBlbGl0LiBNYWVjZW5hcyB';
$data.= 'kaWduaXNzaW0gZHVpIGFjIGxlY3R1cyBwcmV0aXVtIGNvbmRpbWVudHVtLiBNb3JiaSBpZCBpcH';
$data.= 'N1bSBpbiB1cm5hIGVnZXN0YXMgdmFyaXVzIGluIHZpdGFlIHF1YW0uIFBoYXNlbGx1cyBlZmZpY';
$data.= '2l0dXIgZWxlbWVudHVtIHNhcGllbiBpZCBkaWN0dW0u';

$utils = new Utils();
$decoded = $utils->base64UrlDecode($data);

Contributing

Contributions are welcome. Please send your contributions through GitHub pull requests

Pull requests for bug fixes must be based on latest stable release from the master branch whereas pull requests for new features must be based on the developer branch.

Due to time constraints, I'm not always able to respond as quickly as I would like. If you feel you're waiting too long for merging your pull request please remind me here.

Coding standards

We follow PSR-2 coding style and PSR-4 autoloading standards. Be sure you're also following them before sending your pull request.

License

php-json-utils is licensed under the MIT License - see the LICENSE file for details.