php-json-utils
JSON utility class, which provides encoding, decoding, base64url encoding and base64url decoding methods., (*1)
, (*2)
Contents
, (*3)
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:, (*4)
{
"require": {
"mszewcz/php-json-utils": ">=1.0"
}
}
You can also clone or download this respository., (*5)
php-json-utils meets PSR-4 autoloading standards. If using the Composer please include its autoloader file:, (*6)
require_once 'vendor/autoload.php';
If you cloned or downloaded this repository, you will have to code your own PSR-4 style autoloader implementation., (*7)
, (*8)
Usage
To encode data into JSON string:, (*9)
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:, (*10)
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:, (*11)
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:, (*12)
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);
, (*13)
Contributing
Contributions are welcome. Please send your contributions through GitHub pull requests, (*14)
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., (*15)
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 ms here., (*16)
Coding standards
We follow PSR-2 coding style and PSR-4 autoloading standards. Be sure you're also following them before sending us your pull request., (*17)
, (*18)
License
php-json-utils is licensed under the MIT License - see the LICENSE file for details., (*19)