2017 © Pedro Peláez
 

library php-json

Encode, decode, handle errors and valid json with php

image

bernardosecades/php-json

Encode, decode, handle errors and valid json with php

  • Tuesday, August 23, 2016
  • by bernardosecades
  • Repository
  • 2 Watchers
  • 0 Stars
  • 3,395 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 19 % Grown

The README.md

PHP JSON

Build Status Scrutinizer Code Quality Code Coverage License SensioLabsInsight, (*1)

Why?

Mainly to handle errors json encode/decode and encapsulate options to encode easily., (*2)

Usage

use BernardoSecades\Json\Json;

...

Json::decode($value);
Json::encode($value);

Options in json encode:, (*3)

use BernardoSecades\Json\Json;
use BernardoSecades\Json\Option;
use BernardoSecades\Json\ArrayOption;

...

$options = new ArrayOption();
$options[] = Option::JSON_UNESCAPED_UNICODE(); // Use enum object
$options[] = Option::JSON_UNESCAPED_SLASHES();
$options[] = Option::JSON_NUMERIC_CHECK();

Json::encode($value, $options);

Handle errors

use BernardoSecades\Json\Json;
use BernardoSecades\Json\DecodeException;
use BernardoSecades\Json\EncodeException;

...

try {
    Json::decode($value);
} catch (DecodeException $exception) {
    // do something
}

// or

if (!Json::isValid($value)) {
   // do something
}

try {
    Json::encode($value);
} catch (EncodeException $exception) {
 // do something
}

Get info from Encode/Decode exception, (*4)

use BernardoSecades\Json\Json;
use BernardoSecades\Json\DecodeException;

...

try {
    Json::decode($value);
} catch (DecodeException $exception) {

    $errorMessage = sprintf('%s , json error code %d', 
        $exception->getMessage(), // see http://php.net/manual/en/function.json-last-error-msg.php
        $exception->getCode(),    // see http://php.net/manual/en/json.constants.php
    ); 

    $this->logger->error($errorMessage);
    // do something more
}

Installation

composer require bernardosecades/php-json

The Versions

23/08 2016

dev-master

9999999-dev

Encode, decode, handle errors and valid json with php

  Sources   Download

MIT

The Requires

 

The Development Requires

php json encode decode

23/08 2016

v1.0.0

1.0.0.0

Encode, decode, handle errors and valid json with php

  Sources   Download

MIT

The Requires

 

The Development Requires

php json encode decode

23/08 2016

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

Encode, decode, handle errors and valid json with php

  Sources   Download

MIT

The Requires

 

The Development Requires

php json encode decode