57 public const ALL_OK = self::UTF8_OK | self::XMLMETA_OK;
98 public static function encode( $value, $pretty =
false, $escaping = 0 ) {
102 $options = JSON_UNESCAPED_SLASHES;
103 if ( $pretty || is_string( $pretty ) ) {
104 $options |= JSON_PRETTY_PRINT;
106 if ( $escaping & self::UTF8_OK ) {
107 $options |= JSON_UNESCAPED_UNICODE;
109 if ( !( $escaping & self::XMLMETA_OK ) ) {
110 $options |= JSON_HEX_TAG | JSON_HEX_AMP;
112 $json = json_encode( $value, $options );
114 if ( is_string( $pretty ) && $pretty !==
' ' && $json !==
false ) {
119 $json = preg_replace(
'/ {4}|.*+\n\K {4}/A', $pretty, $json );
148 public static function decode( $value, $assoc =
false ) {
149 return json_decode( $value, $assoc );
162 public static function parse( $value, $options = 0 ) {
163 if ( $options & self::STRIP_COMMENTS ) {
164 $value = self::stripComments( $value );
166 $assoc = ( $options & self::FORCE_ASSOC ) !== 0;
167 $result = json_decode( $value, $assoc );
168 $code = json_last_error();
170 if ( $code === JSON_ERROR_SYNTAX && ( $options & self::TRY_FIXING ) !== 0 ) {
179 preg_replace(
'/,([ \t]*[}\]][^"\r\n]*([\r\n]|$)|[ \t]*[\r\n][ \t\r\n]*[}\]])/',
'$1',
180 $value, -1, $count );
182 $result = json_decode( $value, $assoc );
183 if ( json_last_error() === JSON_ERROR_NONE ) {
185 $st = Status::newGood( $result );
186 $st->warning(
wfMessage(
'json-warn-trailing-comma' )->numParams( $count ) );
195 case JSON_ERROR_NONE:
196 return Status::newGood( $result );
198 return Status::newFatal(
wfMessage(
'json-error-unknown' )->numParams( $code ) );
199 case JSON_ERROR_DEPTH:
200 $msg =
'json-error-depth';
202 case JSON_ERROR_STATE_MISMATCH:
203 $msg =
'json-error-state-mismatch';
205 case JSON_ERROR_CTRL_CHAR:
206 $msg =
'json-error-ctrl-char';
208 case JSON_ERROR_SYNTAX:
209 $msg =
'json-error-syntax';
211 case JSON_ERROR_UTF8:
212 $msg =
'json-error-utf8';
214 case JSON_ERROR_INVALID_PROPERTY_NAME:
215 $msg =
'json-error-invalid-property-name';
217 case JSON_ERROR_UTF16:
218 $msg =
'json-error-utf16';
221 return Status::newFatal( $msg );
234 $str = (string)$json;
236 $maxLen = strlen( $str );
243 for ( $idx = 0; $idx < $maxLen; $idx++ ) {
244 switch ( $str[$idx] ) {
246 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
247 if ( !$inComment && $lookBehind !==
'\\' ) {
249 $inString = !$inString;
254 $lookAhead = ( $idx + 1 < $maxLen ) ? $str[$idx + 1] :
'';
255 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
259 } elseif ( !$inComment &&
260 ( $lookAhead ===
'/' || $lookAhead ===
'*' )
264 $buffer .= substr( $str, $mark, $idx - $mark );
269 $multiline = $lookAhead ===
'*';
271 } elseif ( $multiline && $lookBehind ===
'*' ) {
280 if ( $inComment && !$multiline ) {
296 return $buffer . substr( $str, $mark, $maxLen - $mark );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.