A JSON encode/decode helper with easier error handling for PHP
composer require tc/json
<?php
use Tc\Json\Json;
use Tc\Json\JsonException;
$data = [
'foo' => 'bar',
];
$jsonString = Json::encode($data); // {"foo": "bar"}
$jsonDataObject = Json::decode($jsonString); // ( [foo] => bar )
$jsonDataArray = Json::decode($jsonString, true ); // ['foo' => 'bar']
$jsonData = Json::decode($jsonString, true); // ['foo' => 'bar']
try {
Json::decode('{"foo"'); // invalid json string
} catch(JsonException $e) {
echo $e->getMessage(); // description of error
}
Tc Json is licensed with the MIT license.
See LICENSE for more details.