1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Guzzle HTTP JSON-RPC |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @see http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE |
12
|
|
|
* @link http://github.com/graze/guzzle-jsonrpc |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Graze\GuzzleHttp\JsonRpc; |
16
|
|
|
|
17
|
|
|
use Graze\GuzzleHttp\JsonRpc\Exception\JsonDecodeException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Wrapper for JSON decode that implements error detection with helpful |
21
|
|
|
* error messages. |
22
|
|
|
* |
23
|
|
|
* @param string $json JSON data to parse |
24
|
|
|
* @param bool $assoc When true, returned objects will be converted |
25
|
|
|
* into associative arrays. |
26
|
|
|
* @param int $depth User specified recursion depth. |
27
|
|
|
* @param int $options Bitmask of JSON decode options. |
28
|
|
|
* |
29
|
|
|
* @throws JsonDecodeException if the JSON cannot be parsed. |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
* |
33
|
|
|
* @link http://www.php.net/manual/en/function.json-decode.php |
34
|
|
|
* |
35
|
|
|
* @copyright Copyright (c) 2011-2015 Michael Dowling, https://github.com/mtdowling <[email protected]> |
36
|
|
|
* @license MIT https://github.com/guzzle/guzzle/blob/5.3/LICENSE |
37
|
|
|
*/ |
38
|
|
|
function json_decode($json, $assoc = false, $depth = 512, $options = 0) |
39
|
|
|
{ |
40
|
|
|
static $jsonErrors = [ |
41
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
42
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
43
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
44
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
45
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded', |
46
|
25 |
|
]; |
47
|
|
|
|
48
|
|
|
// Patched support for decoding empty strings for PHP 7+ |
49
|
25 |
|
$data = \json_decode($json == "" ? "{}" : $json, $assoc, $depth, $options); |
50
|
|
|
|
51
|
25 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
52
|
|
|
$last = json_last_error(); |
53
|
|
|
$message = 'Unable to parse JSON data: ' . (isset($jsonErrors[$last]) ? $jsonErrors[$last] : 'Unknown error'); |
54
|
|
|
|
55
|
|
|
throw new JsonDecodeException($message, 0, null, $json); |
56
|
|
|
} |
57
|
|
|
|
58
|
25 |
|
return $data; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Wrapper for json_encode that includes character escaping by default. |
63
|
|
|
* |
64
|
|
|
* @param mixed $data |
65
|
|
|
* @param bool $escapeChars |
66
|
|
|
* |
67
|
|
|
* @return string|bool |
68
|
|
|
*/ |
69
|
|
|
function json_encode($data, $escapeChars = true) |
70
|
|
|
{ |
71
|
|
|
$options = |
72
|
3 |
|
JSON_HEX_AMP | |
73
|
3 |
|
JSON_HEX_APOS | |
74
|
3 |
|
JSON_HEX_QUOT | |
75
|
3 |
|
JSON_HEX_TAG | |
76
|
3 |
|
JSON_UNESCAPED_UNICODE | |
77
|
3 |
|
JSON_UNESCAPED_SLASHES; |
78
|
|
|
|
79
|
3 |
|
return \json_encode($data, $escapeChars ? $options : 0); |
80
|
|
|
} |
81
|
|
|
|