cruxoft/bolt-json

JSON wrapper library built on the Bolt Framework

0.4.0 2021-06-24 13:44 UTC

This package is auto-updated.

Last update: 2024-04-24 20:11:28 UTC


README

What is it?

A simple wrapper for dealing with JSON in PHP with built in error handling.

Installation

Can be installed using composer by running the following:

$ composer require cruxoft/bolt-json

Usage

Simple usage involves calling static encode and decode methods however initial work on an OOP interface has been included.

Encode

use Bolt\Json;

try
{
    $json = Json::encode(array("hello" => "world"));
}
catch (\Exception $exception)
{
    die($exception->getMessage());
}

var_dump($json);

Outputs:

string(17) "{"hello":"world"}"

Decode

use Bolt\Json;

try
{
    $json = Json::decode('{"hello":"world"}');
}
catch (\Exception $exception)
{
    die($exception->getMessage());
}

var_dump($json);

Outputs:

object(stdClass)#3 (1) {
  ["hello"]=>
  string(5) "world"
}

Validate

use Bolt\Json;

var_dump(Json::validate('{"hello":"world"}'));
var_dump(Json::validate('{"hello":"world"x}'));

Outputs:

bool(true)
bool(false)

OOP

The class constructor takes either encoded or decoded data and allows manipulation of the data and output.

Very much a work in progress.

use Bolt\Json;

$json = new Json(array("hello" => "world"));
var_dump($json->toString());