1515 */
1616class Helpers
1717{
18- private static $ jsonMessages = [
19- JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded ' ,
20- JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON ' ,
21- JSON_ERROR_CTRL_CHAR => 'Unexpected control character found ' ,
22- JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON ' ,
23- JSON_ERROR_UTF8 => 'Invalid UTF-8 sequence ' ,
24- ];
25-
26-
2718 /** @var Http\IClient */
2819 private static $ client ;
2920
@@ -36,28 +27,10 @@ class Helpers
3627 */
3728 public static function jsonEncode ($ value )
3829 {
39- if (PHP_VERSION_ID < 50500 ) {
40- set_error_handler (function ($ severity , $ message ) { // needed to receive 'recursion detected' error
41- restore_error_handler ();
42- throw new JsonException ($ message );
43- });
44- }
45-
4630 $ json = json_encode ($ value , JSON_UNESCAPED_UNICODE );
47-
48- if (PHP_VERSION_ID < 50500 ) {
49- restore_error_handler ();
50- }
51-
5231 if ($ error = json_last_error ()) {
53- $ message = isset (static ::$ jsonMessages [$ error ])
54- ? static ::$ jsonMessages [$ error ]
55- : (PHP_VERSION_ID >= 50500 ? json_last_error_msg () : 'Unknown error ' );
56-
57- throw new JsonException ($ message , $ error );
32+ throw new JsonException (json_last_error_msg (), $ error );
5833 }
59-
60- $ json = str_replace (array ("\xe2\x80\xa8" , "\xe2\x80\xa9" ), array ('\u2028 ' , '\u2029 ' ), $ json );
6134 return $ json ;
6235 }
6336
@@ -70,16 +43,9 @@ public static function jsonEncode($value)
7043 */
7144 public static function jsonDecode ($ json )
7245 {
73- $ json = (string ) $ json ;
74- if (!preg_match ('##u ' , $ json )) {
75- throw new JsonException ('Invalid UTF-8 sequence ' , 5 ); // PECL JSON-C
76- }
77-
78- $ value = json_decode ($ json , false , 512 , (defined ('JSON_C_VERSION ' ) && PHP_INT_SIZE > 4 ) ? 0 : JSON_BIGINT_AS_STRING );
79-
80- if ($ value === null && $ json !== '' && strcasecmp ($ json , 'null ' )) { // '' does not clear json_last_error()
81- $ error = json_last_error ();
82- throw new JsonException (isset (static ::$ jsonMessages [$ error ]) ? static ::$ jsonMessages [$ error ] : 'Unknown error ' , $ error );
46+ $ value = json_decode ((string ) $ json , false , 512 , JSON_BIGINT_AS_STRING );
47+ if ($ error = json_last_error ()) {
48+ throw new JsonException (json_last_error_msg (), $ error );
8349 }
8450 return $ value ;
8551 }
0 commit comments