55 public const ALL_OK = self::UTF8_OK | self::XMLMETA_OK;
96 public static function encode( $value, $pretty =
false, $escaping = 0 ) {
100 $options = JSON_UNESCAPED_SLASHES;
101 if ( $pretty || is_string( $pretty ) ) {
102 $options |= JSON_PRETTY_PRINT;
104 if ( $escaping & self::UTF8_OK ) {
105 $options |= JSON_UNESCAPED_UNICODE;
107 if ( !( $escaping & self::XMLMETA_OK ) ) {
108 $options |= JSON_HEX_TAG | JSON_HEX_AMP;
110 $json = json_encode( $value, $options );
112 if ( is_string( $pretty ) && $pretty !==
' ' && $json !==
false ) {
117 $json = preg_replace(
'/ {4}|.*+\n\K {4}/A', $pretty, $json );
146 public static function decode( $value, $assoc =
false ) {
147 return json_decode( $value, $assoc );
160 public static function parse( $value, $options = 0 ) {
161 if ( $options & self::STRIP_COMMENTS ) {
162 $value = self::stripComments( $value );
164 $assoc = ( $options & self::FORCE_ASSOC ) !== 0;
165 $result = json_decode( $value, $assoc );
166 $code = json_last_error();
168 if ( $code === JSON_ERROR_SYNTAX && ( $options & self::TRY_FIXING ) !== 0 ) {
177 preg_replace(
'/,([ \t]*[}\]][^"\r\n]*([\r\n]|$)|[ \t]*[\r\n][ \t\r\n]*[}\]])/',
'$1',
178 $value, -1, $count );
180 $result = json_decode( $value, $assoc );
181 if ( json_last_error() === JSON_ERROR_NONE ) {
183 $st = Status::newGood( $result );
184 $st->warning(
wfMessage(
'json-warn-trailing-comma' )->numParams( $count ) );
194 case JSON_ERROR_NONE:
195 return Status::newGood( $result );
197 return Status::newFatal(
wfMessage(
'json-error-unknown' )->numParams( $code ) );
198 case JSON_ERROR_DEPTH:
199 $msg =
'json-error-depth';
201 case JSON_ERROR_STATE_MISMATCH:
202 $msg =
'json-error-state-mismatch';
204 case JSON_ERROR_CTRL_CHAR:
205 $msg =
'json-error-ctrl-char';
207 case JSON_ERROR_SYNTAX:
208 $msg =
'json-error-syntax';
210 case JSON_ERROR_UTF8:
211 $msg =
'json-error-utf8';
213 case JSON_ERROR_UTF16:
214 $msg =
'json-error-utf16';
217 return Status::newFatal( $msg );
230 $str = (string)$json;
232 $maxLen = strlen( $str );
239 for ( $idx = 0; $idx < $maxLen; $idx++ ) {
240 switch ( $str[$idx] ) {
242 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
243 if ( !$inComment && $lookBehind !==
'\\' ) {
245 $inString = !$inString;
250 $lookAhead = ( $idx + 1 < $maxLen ) ? $str[$idx + 1] :
'';
251 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
255 } elseif ( !$inComment &&
256 ( $lookAhead ===
'/' || $lookAhead ===
'*' )
260 $buffer .= substr( $str, $mark, $idx - $mark );
265 $multiline = $lookAhead ===
'*';
267 } elseif ( $multiline && $lookBehind ===
'*' ) {
276 if ( $inComment && !$multiline ) {
292 return $buffer . substr( $str, $mark, $maxLen - $mark );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.