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 ) );
193 case JSON_ERROR_NONE:
194 return Status::newGood( $result );
196 return Status::newFatal(
wfMessage(
'json-error-unknown' )->numParams( $code ) );
197 case JSON_ERROR_DEPTH:
198 $msg =
'json-error-depth';
200 case JSON_ERROR_STATE_MISMATCH:
201 $msg =
'json-error-state-mismatch';
203 case JSON_ERROR_CTRL_CHAR:
204 $msg =
'json-error-ctrl-char';
206 case JSON_ERROR_SYNTAX:
207 $msg =
'json-error-syntax';
209 case JSON_ERROR_UTF8:
210 $msg =
'json-error-utf8';
212 case JSON_ERROR_INVALID_PROPERTY_NAME:
213 $msg =
'json-error-invalid-property-name';
215 case JSON_ERROR_UTF16:
216 $msg =
'json-error-utf16';
219 return Status::newFatal( $msg );
232 $str = (string)$json;
234 $maxLen = strlen( $str );
241 for ( $idx = 0; $idx < $maxLen; $idx++ ) {
242 switch ( $str[$idx] ) {
244 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
245 if ( !$inComment && $lookBehind !==
'\\' ) {
247 $inString = !$inString;
252 $lookAhead = ( $idx + 1 < $maxLen ) ? $str[$idx + 1] :
'';
253 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
257 } elseif ( !$inComment &&
258 ( $lookAhead ===
'/' || $lookAhead ===
'*' )
262 $buffer .= substr( $str, $mark, $idx - $mark );
267 $multiline = $lookAhead ===
'*';
269 } elseif ( $multiline && $lookBehind ===
'*' ) {
278 if ( $inComment && !$multiline ) {
294 return $buffer . substr( $str, $mark, $maxLen - $mark );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.