2017 © Pedro Peláez
 

library json_parser

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails

image

mihasicehcek/json_parser

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails

  • Friday, March 17, 2017
  • by mihasicehcek
  • Repository
  • 1 Watchers
  • 0 Stars
  • 802 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

JsonParser

Latest Stable Version Total Downloads License, (*1)

Installation

With Composer

$ composer require mihasicehcek/json_parser

Simple Eample

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails., (*2)

try{
  $json = '{"integer":1';
  $result = \JsonParser\JsonParser::jsonDecode('{"integer":1');
}catch(\JsonParser\Exception $ex){
  print_r("Message: ".$ex->getMessage().", code: ".$ex->getCode()); //Message: Syntax error, code: 4
}

try{
  $a = new stdClass();
  $b = new stdClass();
  $a->b = $b;
  $b->a = $a;
  $result = \JsonParser\JsonParser::jsonEncode($a);
}catch(\JsonParser\Exception $ex){
  print_r("Message: ".$ex->getMessage().", code: ".$ex->getCode()); //Message: The object or array passed to json_encode include recursive references and cannot be encoded, code: 7
}

Library has two public methods \JsonParser\JsonParser::jsonDecode() for serialization and \JsonParser\JsonParser::jsonEncode() for deserialization. Signature of these method same as json_decode and json_encode. But unlike native function library throws exceptions when decoding/encoding went wrong. There are exceptions for each de/serialization error. Full list of error you can see at json_last_error, (*3)

Exceptions

Library detects and throws such exceptions, (*4)

  • \JsonParser\SyntaxException - Syntax error.
  • \JsonParser\ControlCharacterException - Control character error, possibly incorrectly encoded.
  • \JsonParser\DepthException - The maximum stack depth has been exceeded.
  • \JsonParser\InfOrNanException - The value passed to json_encode() includes either NAN or INF.
  • \JsonParser\InvalidPropertyNameException - A key starting with \u0000 character was in the string passed to json_decode() when decoding a JSON object into a PHP object.
  • \JsonParser\RecursionException - The object or array passed to json_encode include recursive references and cannot be encoded.
  • \JsonParser\StateMismatchException - State mismatch (invalid or malformed JSON).
  • \JsonParser\UnsupportedTypeException - A value of an unsupported type was given to json_encode(), such as a resource.
  • \JsonParser\Utf8Exception - Malformed UTF-8 characters, possibly incorrectly encoded.
  • \JsonParser\Utf16Exception - Single unpaired UTF-16 surrogate in unicode escape contained in the JSON string passed to json_encode().

All those exceptions extends base \JsonParser\Exception, (*5)

The Versions

17/03 2017

dev-master

9999999-dev

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

The Development Requires

17/03 2017

v1.0.2

1.0.2.0

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

The Development Requires

06/02 2017

v1.0.1

1.0.1.0

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

The Development Requires

18/01 2017

v1.0.0

1.0.0.0

Simple wrapper over json_decode/json_encode php functions. Throws exceptions when decoding/encoding fails

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

The Development Requires