Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
json.php
1
<?php
2
3
namespace
Bitrix\Main\Web
;
4
5
use
Bitrix\Main\Application
;
6
use
Bitrix\Main\Text\Encoding
;
7
use
Bitrix\Main\ArgumentException
;
8
use
Bitrix\Main\Context
;
9
10
class
Json
11
{
12
const
JSON_ERROR_UNKNOWN
= -1;
13
24
public
static
function
encode
($data, $options =
null
)
25
{
26
if
(!
Application::getInstance
()->isUtfMode())
27
{
28
self::serializeJson
($data);
29
$data =
self::convertData
($data);
30
}
31
32
if
(is_null($options))
33
{
34
$options = JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT;
35
}
36
37
$res = json_encode($data, $options);
38
39
self::checkException
($options);
40
41
return
$res;
42
}
43
53
public
static
function
decode
($data)
54
{
55
$res = json_decode($data,
true
);
56
57
self::checkException
();
58
59
// PHP<5.3.3 returns no error for JSON_ERROR_UTF8 and some other ones
60
if
($res ===
null
&& strtolower($data) !=
'null'
)
61
{
62
self::throwException
(self::JSON_ERROR_UNKNOWN);
63
}
64
65
if
(!
Application::getInstance
()->isUtfMode())
66
{
67
$res =
self::unConvertData
($res);
68
}
69
70
return
$res;
71
}
72
79
protected
static
function
serializeJson
(&$data)
80
{
81
if
($data instanceof \JsonSerializable)
82
{
83
$data = $data->jsonSerialize();
84
}
85
86
if
(is_iterable($data))
87
{
88
foreach
($data as $key => $value)
89
{
90
self::serializeJson
($data[$key]);
91
}
92
}
93
}
94
101
protected
static
function
convertData
($data)
102
{
103
$culture =
Context::getCurrent
()->getCulture();
104
105
return
Encoding::convertEncoding($data, $culture->getCharset(),
'UTF-8'
);
106
}
107
114
protected
static
function
unConvertData
($data)
115
{
116
$culture =
Context::getCurrent
()->getCulture();
117
118
return
Encoding::convertEncoding($data,
'UTF-8'
, $culture->getCharset());
119
}
120
129
protected
static
function
checkException
($options = 0)
130
{
131
$e = json_last_error();
132
133
if
($e == JSON_ERROR_NONE)
134
{
135
return
;
136
}
137
138
if
($e == JSON_ERROR_UTF8 && ($options & JSON_PARTIAL_OUTPUT_ON_ERROR))
139
{
140
return
;
141
}
142
143
$message = sprintf(
'%s [%d]'
, json_last_error_msg(), $e);
144
self::throwException
($message);
145
}
146
155
protected
static
function
throwException
($e)
156
{
157
throw
new
ArgumentException
(
'JSON error: '
.$e,
'data'
);
158
}
159
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Context\getCurrent
static getCurrent()
Definition
context.php:241
Bitrix\Main\Text\Encoding
Definition
encoding.php:8
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Main\Web\Json\throwException
static throwException($e)
Definition
json.php:155
Bitrix\Main\Web\Json\JSON_ERROR_UNKNOWN
const JSON_ERROR_UNKNOWN
Definition
json.php:12
Bitrix\Main\Web\Json\decode
static decode($data)
Definition
json.php:53
Bitrix\Main\Web\Json\serializeJson
static serializeJson(&$data)
Definition
json.php:79
Bitrix\Main\Web\Json\checkException
static checkException($options=0)
Definition
json.php:129
Bitrix\Main\Web\Json\convertData
static convertData($data)
Definition
json.php:101
Bitrix\Main\Web\Json\unConvertData
static unConvertData($data)
Definition
json.php:114
Bitrix\Main\Web\Json\encode
static encode($data, $options=null)
Definition
json.php:24
Bitrix\Main\Context
Definition
culture.php:9
Bitrix\Main\Web
Definition
cookie.php:3
modules
main
lib
web
json.php
Создано системой
1.10.0