|
1
|
|
|
<?php |
|
2
|
|
|
namespace Crossjoin\Json; |
|
3
|
|
|
|
|
4
|
|
|
use Crossjoin\Json\Exception\InvalidArgumentException; |
|
5
|
|
|
use Crossjoin\Json\Exception\JsonException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Returns the JSON representation of a value, encoded in UTF-8 |
|
9
|
|
|
* (or false on failure) |
|
10
|
|
|
* |
|
11
|
|
|
* @param mixed $value |
|
12
|
|
|
* @param int $options |
|
13
|
|
|
* @param int $depth |
|
14
|
|
|
* |
|
15
|
|
|
* For details about the parameters and the return values see the native json_encode() function. |
|
16
|
|
|
* @link http://php.net/manual/en/function.json-encode.php |
|
17
|
|
|
* |
|
18
|
|
|
* @return string|false |
|
19
|
|
|
* @throws \Crossjoin\Json\Exception\InvalidArgumentException |
|
20
|
|
|
*/ |
|
21
|
|
|
function json_encode($value, $options = 0, $depth = 512) |
|
22
|
|
|
{ |
|
23
|
|
|
$encoder = new Encoder(); |
|
24
|
|
|
try { |
|
25
|
|
|
return $encoder->encode($value, $options, $depth); |
|
26
|
|
|
} catch (InvalidArgumentException $e) { |
|
27
|
|
|
throw $e; |
|
28
|
|
|
} catch (JsonException $e) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @noinspection MoreThanThreeArgumentsInspection */ |
|
34
|
|
|
/** |
|
35
|
|
|
* Decodes a JSON string (encoded as UTF-8, UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE, |
|
36
|
|
|
* with or without byte order mark) |
|
37
|
|
|
* |
|
38
|
|
|
* @param mixed $json |
|
39
|
|
|
* @param bool $assoc |
|
40
|
|
|
* @param int $depth |
|
41
|
|
|
* @param int $options |
|
42
|
|
|
* |
|
43
|
|
|
* For details about the parameters and the return values see the native json_decode() function. |
|
44
|
|
|
* @link http://php.net/manual/en/function.json-decode.php |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
* @throws \Crossjoin\Json\Exception\InvalidArgumentException |
|
48
|
|
|
*/ |
|
49
|
|
|
function json_decode($json, $assoc = false, $depth = 512, $options = 0) |
|
50
|
|
|
{ |
|
51
|
|
|
$decoder = new Decoder(); |
|
52
|
|
|
try { |
|
53
|
|
|
return $decoder->decode($json, $assoc, $depth, $options); |
|
54
|
|
|
} catch (InvalidArgumentException $e) { |
|
55
|
|
|
throw $e; |
|
56
|
|
|
} catch (JsonException $e) { |
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the code of the last error occurred |
|
63
|
|
|
* |
|
64
|
|
|
* For details about the return values see the native json_last_error() function |
|
65
|
|
|
* @link http://php.net/manual/en/function.json-last-error.php |
|
66
|
|
|
* |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
|
|
function json_last_error () { |
|
70
|
|
|
return \json_last_error(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the error string of the last json_encode() or json_decode() call |
|
75
|
|
|
* |
|
76
|
|
|
* For details about the return values see the native json_last_error_msg() function |
|
77
|
|
|
* @link http://php.net/manual/en/function.json-last-error-msg.php |
|
78
|
|
|
* |
|
79
|
|
|
* @return string|false |
|
80
|
|
|
*/ |
|
81
|
|
|
function json_last_error_msg () { |
|
82
|
|
|
if (function_exists('\json_last_error_msg')) { |
|
83
|
|
|
return \json_last_error_msg(); |
|
84
|
|
|
} |
|
85
|
|
|
return 'An error occurred while encoding or decoding JSON.'; |
|
86
|
|
|
} |
|
87
|
|
|
|