55 const ALL_OK = self::UTF8_OK | self::XMLMETA_OK;
115 public static function encode( $value, $pretty =
false, $escaping = 0 ) {
116 if ( !is_string( $pretty ) ) {
117 $pretty = $pretty ?
' ' :
false;
123 $options = JSON_UNESCAPED_SLASHES;
124 $options |= $pretty !==
false ? JSON_PRETTY_PRINT : 0;
125 $options |= ( $escaping & self::UTF8_OK ) ? JSON_UNESCAPED_UNICODE : 0;
126 $options |= ( $escaping & self::XMLMETA_OK ) ? 0 : ( JSON_HEX_TAG | JSON_HEX_AMP );
127 $json = json_encode( $value, $options );
128 if ( $json ===
false ) {
132 if ( $pretty !==
false && $pretty !==
' ' ) {
134 $json = str_replace(
"\n ",
"\n\t", $json );
135 while ( strpos( $json,
"\t " ) !==
false ) {
136 $json = str_replace(
"\t ",
"\t\t", $json );
139 if ( $pretty !==
"\t" ) {
141 $json = str_replace(
"\t", $pretty, $json );
144 if ( $escaping & self::UTF8_OK ) {
145 $json = str_replace( self::$badChars, self::$badCharsEscaped, $json );
174 public static function decode( $value, $assoc =
false ) {
175 return json_decode( $value, $assoc );
188 public static function parse( $value, $options = 0 ) {
189 if ( $options & self::STRIP_COMMENTS ) {
190 $value = self::stripComments( $value );
192 $assoc = ( $options & self::FORCE_ASSOC ) !== 0;
193 $result = json_decode( $value, $assoc );
194 $code = json_last_error();
196 if ( $code === JSON_ERROR_SYNTAX && ( $options & self::TRY_FIXING ) !== 0 ) {
205 preg_replace(
'/,([ \t]*[}\]][^"\r\n]*([\r\n]|$)|[ \t]*[\r\n][ \t\r\n]*[}\]])/',
'$1',
206 $value, -1, $count );
208 $result = json_decode( $value, $assoc );
209 if ( JSON_ERROR_NONE === json_last_error() ) {
211 $st = Status::newGood( $result );
212 $st->warning(
wfMessage(
'json-warn-trailing-comma' )->numParams( $count ) );
219 case JSON_ERROR_NONE:
220 return Status::newGood( $result );
222 return Status::newFatal(
wfMessage(
'json-error-unknown' )->numParams( $code ) );
223 case JSON_ERROR_DEPTH:
224 $msg =
'json-error-depth';
226 case JSON_ERROR_STATE_MISMATCH:
227 $msg =
'json-error-state-mismatch';
229 case JSON_ERROR_CTRL_CHAR:
230 $msg =
'json-error-ctrl-char';
232 case JSON_ERROR_SYNTAX:
233 $msg =
'json-error-syntax';
235 case JSON_ERROR_UTF8:
236 $msg =
'json-error-utf8';
238 case JSON_ERROR_RECURSION:
239 $msg =
'json-error-recursion';
241 case JSON_ERROR_INF_OR_NAN:
242 $msg =
'json-error-inf-or-nan';
244 case JSON_ERROR_UNSUPPORTED_TYPE:
245 $msg =
'json-error-unsupported-type';
248 return Status::newFatal( $msg );
261 $str = (string)$json;
263 $maxLen = strlen( $str );
270 for ( $idx = 0; $idx < $maxLen; $idx++ ) {
271 switch ( $str[$idx] ) {
273 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
274 if ( !$inComment && $lookBehind !==
'\\' ) {
276 $inString = !$inString;
281 $lookAhead = ( $idx + 1 < $maxLen ) ? $str[$idx + 1] :
'';
282 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
286 } elseif ( !$inComment &&
287 ( $lookAhead ===
'/' || $lookAhead ===
'*' )
291 $buffer .= substr( $str, $mark, $idx - $mark );
296 $multiline = $lookAhead ===
'*';
298 } elseif ( $multiline && $lookBehind ===
'*' ) {
307 if ( $inComment && !$multiline ) {
323 return $buffer . substr( $str, $mark, $maxLen - $mark );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.