Bitrix-D7  20.0.0
web/json.php
См. документацию.
1 <?php
2 namespace Bitrix\Main\Web;
3 
4 use \Bitrix\Main\Application;
5 use \Bitrix\Main\Text\Encoding;
6 use \Bitrix\Main\ArgumentException;
7 
8 class Json
9 {
10  const JSON_ERROR_UNKNOWN = -1;
11 
12  /**
13  * Returns a string containing the JSON representation of $data.
14  *
15  * @param mixed $data The value being encoded.
16  * @param null $options Bitmasked options. Default is JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT.
17  *
18  * @return mixed
19  * @throws \Bitrix\Main\ArgumentException
20  * @see json_encode
21  */
22  public static function encode($data, $options = null)
23  {
24  if (!Application::getInstance()->isUtfMode())
25  {
26  self::serializeJson($data);
27  $data = self::convertData($data);
28  }
29 
30  if (is_null($options))
31  {
32  $options = JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT;
33  }
34 
35  $res = json_encode($data, $options);
36 
37  self::checkException($options);
38 
39  return $res;
40  }
41 
42  /**
43  * Takes a JSON encoded string and converts it into a PHP variable.
44  *
45  * @param string $data The json string being decoded.
46  *
47  * @return mixed
48  * @throws \Bitrix\Main\ArgumentException
49  * @see json_decode
50  */
51  public static function decode($data)
52  {
53  $res = json_decode($data, true);
54 
56 
57  // PHP<5.3.3 returns no error for JSON_ERROR_UTF8 and some other ones
58  if($res === null && ToLower($data) != 'null')
59  {
60  self::throwException(self::JSON_ERROR_UNKNOWN);
61  }
62 
63  if (!Application::getInstance()->isUtfMode())
64  {
65  $res = self::unConvertData($res);
66  }
67 
68  return $res;
69  }
70 
71  /**
72  * Executes serializeJson on JsonSerializable objects for non-UTF8 instances.
73  * We have to do it manually to prevent "malformed UTF-8 characters" error.
74  *
75  * @param mixed $data
76  */
77  protected static function serializeJson(&$data)
78  {
79  if($data instanceof \JsonSerializable)
80  {
81  $data = $data->jsonSerialize();
82  }
83  else if (is_iterable($data))
84  {
85  foreach ($data as $key => $value)
86  {
87  self::serializeJson($data[$key]);
88  }
89  }
90  }
91 
92  /**
93  * Converts $data to UTF-8 charset.
94  *
95  * @param mixed $data Input data.
96  * @return mixed
97  */
98  protected static function convertData($data)
99  {
100  return Encoding::convertEncoding($data, SITE_CHARSET, 'UTF-8');
101  }
102 
103  /**
104  * Converts $data from UTF-8 charset.
105  *
106  * @param mixed $data Input data.
107  * @return mixed
108  */
109  protected static function unConvertData($data)
110  {
111  return Encoding::convertEncoding($data, 'UTF-8', SITE_CHARSET);
112  }
113 
114  /**
115  * Checks global error flag and raises exception if needed.
116  *
117  * @param integer $options Bitmasked options. When JSON_PARTIAL_OUTPUT_ON_ERROR passed no exception is raised.
118  *
119  * @return void
120  * @throws \Bitrix\Main\ArgumentException
121  */
122  protected static function checkException($options = 0)
123  {
124  $e = json_last_error();
125 
126  if ($e == JSON_ERROR_NONE)
127  {
128  return;
129  }
130 
131  if ($e == JSON_ERROR_UTF8 && ($options & JSON_PARTIAL_OUTPUT_ON_ERROR))
132  {
133  return;
134  }
135 
136  if (function_exists('json_last_error_msg'))
137  {
138  // Must be available on PHP >= 5.5.0
139  $message = sprintf('%s [%d]', json_last_error_msg(), $e);
140  }
141  else
142  {
143  $message = $e;
144  }
145 
146  self::throwException($message);
147  }
148 
149  /**
150  * Throws exception with message given.
151  *
152  * @param string $e Exception message.
153  *
154  * @return void
155  * @throws \Bitrix\Main\ArgumentException
156  */
157  protected static function throwException($e)
158  {
159  throw new ArgumentException('JSON error: '.$e, 'data');
160  }
161 }
Bitrix\Main\Web\Json\serializeJson
static serializeJson(&$data)
Executes serializeJson on JsonSerializable objects for non-UTF8 instances.
Definition: web/json.php:77
Bitrix\Main\Web\Json\JSON_ERROR_UNKNOWN
const JSON_ERROR_UNKNOWN
Definition: web/json.php:10
Bitrix\Main\Web\Json\encode
static encode($data, $options=null)
Returns a string containing the JSON representation of $data.
Definition: web/json.php:22
Bitrix\Main\ArgumentException
Exception is thrown when function argument is not valid.
Definition: main/lib/exception.php:33
Bitrix\Main\Web\Json\checkException
static checkException($options=0)
Checks global error flag and raises exception if needed.
Definition: web/json.php:122
Bitrix\Main\Web\Json\decode
static decode($data)
Takes a JSON encoded string and converts it into a PHP variable.
Definition: web/json.php:51
Bitrix\Main\Web\Json
Definition: web/json.php:8
Bitrix\Main\Web\Json\throwException
static throwException($e)
Throws exception with message given.
Definition: web/json.php:157
Bitrix\Main\Web\Json\unConvertData
static unConvertData($data)
Converts $data from UTF-8 charset.
Definition: web/json.php:109
Bitrix\Main\Application\getInstance
static getInstance()
Returns current instance of the Application.
Definition: main/lib/application.php:86
Bitrix\Main\Text\Encoding\convertEncoding
static convertEncoding($data, $charsetFrom, $charsetTo, &$errorMessage="")
Converts data from a source encoding to a target encoding.
Definition: encoding.php:53
Bitrix\Main\Web\Json\convertData
static convertData($data)
Converts $data to UTF-8 charset.
Definition: web/json.php:98
Bitrix\Main\Web
Definition: cookie.php:2