Skip to content

Commit a354294

Browse files
committed
Initial commit
0 parents  commit a354294

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
.idea

composer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "daverandom/exceptional-json",
3+
"description": "JSON encoding and decoding that throws exceptions on failure",
4+
"minimum-stability": "stable",
5+
"license": "MIT",
6+
"authors": [{"name": "Chris Wright"}],
7+
"autoload": {
8+
"psr-4": {"ExceptionalJSON\\": "src/"},
9+
"files": ["src/functions.php"]
10+
}
11+
}

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Exceptional JSON
2+
3+
Thin wrapper around PHP's `json_encode()` and `json_decode()` functions, which throws exceptions when an operation fails.
4+
5+
**Required PHP Version**
6+
7+
- PHP 7.0+
8+
9+
**Installation**
10+
11+
```bash
12+
$ composer require amphp/amp
13+
```
14+
15+
**Usage**
16+
17+
Call the `\ExceptionJSON\encode()` and `\ExceptionJSON\decode()` functions in exactly the same way as you would with
18+
`json_encode()` and `json_decode()`. The only difference is that they will throw an exception if the operation fails.
19+
20+
Also defines `json_try_encode()` and `json_try_decode()` in the root namespace if they don't already exist, these are
21+
simply aliases of their namespaced counterparts.
22+
23+
```php
24+
$encoded = \ExceptionJSON\encode($data);
25+
$decoded = \ExceptionJSON\decode($encoded);
26+
```
27+
28+
**What about tests?**
29+
30+
![Tests? Where we're going, we don't need... tests](https://i.imgflip.com/13kfdv.jpg)

src/DecodeErrorException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ExceptionalJSON;
4+
5+
/**
6+
* Exception thrown when a JSON decoding operation fails.
7+
*
8+
* @package ExceptionalJSON
9+
*/
10+
class DecodeErrorException extends Exception
11+
{
12+
private $json;
13+
14+
public function __construct(int $code, string $message, string $json)
15+
{
16+
parent::__construct($message, $code);
17+
$this->json = $json;
18+
}
19+
20+
public function getJSON(): string
21+
{
22+
return $this->json;
23+
}
24+
}

src/EncodeErrorException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ExceptionalJSON;
4+
5+
/**
6+
* Exception thrown when a JSON decoding operation fails.
7+
*
8+
* @package ExceptionalJSON
9+
*/
10+
class EncodeErrorException extends Exception
11+
{
12+
private $value;
13+
14+
public function __construct(int $code, string $message, $value)
15+
{
16+
parent::__construct($message, $code);
17+
$this->value = $value;
18+
}
19+
20+
public function getValue(): string
21+
{
22+
return $this->value;
23+
}
24+
}

src/Exception.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ExceptionalJSON;
4+
5+
/**
6+
* Base exception thrown when a JSON operation fails.
7+
*
8+
* @package ExceptionalJSON
9+
*/
10+
abstract class Exception extends \RuntimeException
11+
{
12+
public function __construct(int $code, string $message)
13+
{
14+
parent::__construct($message, $code);
15+
}
16+
}

src/functions.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ExceptionalJSON
4+
{
5+
/**
6+
* Decodes a JSON string.
7+
*
8+
* @param string $json The JSON string being decoded.
9+
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays.
10+
* @param int $depth User specified recursion depth.
11+
* @param int $options Bit mask of JSON decode options.
12+
* @return mixed The value encoded in JSON in appropriate PHP type.
13+
* @throws DecodeErrorException When the decode operation fails.
14+
*/
15+
function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
16+
{
17+
$result = \json_decode($json, $assoc, $depth, $options);
18+
$code = \json_last_error();
19+
20+
if ($code !== \JSON_ERROR_NONE) {
21+
throw new DecodeErrorException($code, \json_last_error_msg(), $json);
22+
}
23+
24+
return $result;
25+
}
26+
27+
/**
28+
* Returns the JSON representation of a value.
29+
*
30+
* @param mixed $value The value being encoded.
31+
* @param int $depth User specified recursion depth.
32+
* @param int $options Bit mask of JSON encode options.
33+
* @return string JSON encoded string.
34+
* @throws Exception When the encode operation fails
35+
*/
36+
function encode($value, int $options = 0, int $depth = 512)
37+
{
38+
$result = \json_encode($value, $options, $depth);
39+
$code = \json_last_error();
40+
41+
if ($code !== \JSON_ERROR_NONE) {
42+
throw new EncodeErrorException($code, \json_last_error_msg(), $value);
43+
}
44+
45+
return $result;
46+
}
47+
}
48+
49+
namespace
50+
{
51+
use function ExceptionalJSON\decode;
52+
use function ExceptionalJSON\encode;
53+
54+
if (!function_exists('json_try_decode')) {
55+
/**
56+
* Decodes a JSON string.
57+
*
58+
* @param string $json The JSON string being decoded.
59+
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays.
60+
* @param int $depth User specified recursion depth.
61+
* @param int $options Bit mask of JSON decode options.
62+
* @return mixed The value encoded in JSON in appropriate PHP type.
63+
* @throws \ExceptionalJSON\DecodeErrorException When the decode operation fails.
64+
*/
65+
function json_try_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
66+
{
67+
return decode($json, $assoc, $depth, $options);
68+
}
69+
}
70+
71+
if (!function_exists('json_try_encode')) {
72+
/**
73+
* Returns the JSON representation of a value.
74+
*
75+
* @param mixed $value The value being encoded.
76+
* @param int $depth User specified recursion depth.
77+
* @param int $options Bit mask of JSON encode options.
78+
* @return string JSON encoded string.
79+
* @throws \ExceptionalJSON\EncodeErrorException When the encode operation fails
80+
*/
81+
function json_try_encode($value, int $options = 0, int $depth = 512)
82+
{
83+
return encode($value, $options, $depth);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)