82publicstaticfunctionencode( $value, $pretty =
false, $escaping = 0 ) {
83// PHP escapes '/' to prevent breaking out of inline script blocks using '</script>', 84// which is hardly useful when '<' and '>' are escaped (and inadequate), and such 85// escaping negatively impacts the human readability of URLs and similar strings. 86 $options = JSON_UNESCAPED_SLASHES;
87if ( $pretty || is_string( $pretty ) ) {
88 $options |= JSON_PRETTY_PRINT;
90if ( $escaping & self::UTF8_OK ) {
91 $options |= JSON_UNESCAPED_UNICODE;
93if ( !( $escaping & self::XMLMETA_OK ) ) {
94 $options |= JSON_HEX_TAG | JSON_HEX_AMP;
96 $json = json_encode( $value, $options );
98if ( is_string( $pretty ) && $pretty !==
' ' && $json !==
false ) {
99// Change the four-space indent to the provided indent. 100// The regex matches four spaces either at the start of a line or immediately 101// after the previous match. $pretty should contain only whitespace characters, 102// so there should be no need to call StringUtils::escapeRegexReplacement(). 103 $json = preg_replace(
'/ {4}|.*+\n\K {4}/A', $pretty, $json );
132publicstaticfunctiondecode( $value, $assoc =
false ) {
133return json_decode( $value, $assoc );
146publicstaticfunctionparse( $value, $options = 0 ) {
147if ( $options & self::STRIP_COMMENTS ) {
151 $result = json_decode( $value, $assoc );
152 $code = json_last_error();
154if ( $code === JSON_ERROR_SYNTAX && ( $options & self::TRY_FIXING ) !== 0 ) {
155// The most common error is the trailing comma in a list or an object. 156// We cannot simply replace /,\s*[}\]]/ because it could be inside a string value. 157// But we could use the fact that JSON does not allow multi-line string values, 158// And remove trailing commas if they are et the end of a line. 159// JSON only allows 4 control characters: [ \t\r\n]. So we must not use '\s' for matching. 160// Regex match ,]<any non-quote chars>\n or ,\n] with optional spaces/tabs. 163 preg_replace(
'/,([ \t]*[}\]][^"\r\n]*([\r\n]|$)|[ \t]*[\r\n][ \t\r\n]*[}\]])/',
'$1',
164 $value, -1, $count );
166 $result = json_decode( $value, $assoc );
167if ( json_last_error() === JSON_ERROR_NONE ) {
169 $st = Status::newGood( $result );
170 $st->warning(
wfMessage(
'json-warn-trailing-comma' )->numParams( $count ) );
176// JSON_ERROR_RECURSION, JSON_ERROR_INF_OR_NAN, JSON_ERROR_UNSUPPORTED_TYPE, 177// are all encode errors that we don't need to care about here. 180return Status::newGood( $result );
182return Status::newFatal(
wfMessage(
'json-error-unknown' )->numParams( $code ) );
183case JSON_ERROR_DEPTH:
184 $msg =
'json-error-depth';
186case JSON_ERROR_STATE_MISMATCH:
187 $msg =
'json-error-state-mismatch';
189case JSON_ERROR_CTRL_CHAR:
190 $msg =
'json-error-ctrl-char';
192case JSON_ERROR_SYNTAX:
193 $msg =
'json-error-syntax';
196 $msg =
'json-error-utf8';
198case JSON_ERROR_INVALID_PROPERTY_NAME:
199 $msg =
'json-error-invalid-property-name';
201case JSON_ERROR_UTF16:
202 $msg =
'json-error-utf16';
205return Status::newFatal( $msg );
217// Ensure we have a string 218 $str = (string)$json;
220 $maxLen = strlen( $str );
227for ( $idx = 0; $idx < $maxLen; $idx++ ) {
228switch ( $str[$idx] ) {
230 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
231if ( !$inComment && $lookBehind !==
'\\' ) {
232// Either started or ended a string 233 $inString = !$inString;
238 $lookAhead = ( $idx + 1 < $maxLen ) ? $str[$idx + 1] :
'';
239 $lookBehind = ( $idx - 1 >= 0 ) ? $str[$idx - 1] :
'';
243 } elseif ( !$inComment &&
244 ( $lookAhead ===
'/' || $lookAhead ===
'*' )
246// Transition into a comment 247// Add characters seen to buffer 248 $buffer .= substr( $str, $mark, $idx - $mark );
249// Consume the look ahead character 253 $multiline = $lookAhead ===
'*';
255 } elseif ( $multiline && $lookBehind ===
'*' ) {
256// Found the end of the current comment 264if ( $inComment && !$multiline ) {
265// Found the end of the current comment 273// Comment ends with input 274// Technically we should check to ensure that we aren't in 275// a multiline comment that hasn't been properly ended, but this 276// is a strip filter, not a validating parser. 279// Add final chunk to buffer before returning 280return $buffer . substr( $str, $mark, $maxLen - $mark );
284class_alias( FormatJson::class,
'FormatJson' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
JSON formatter wrapper class.
static decode( $value, $assoc=false)
Decodes a JSON string.
const ALL_OK
Skip escaping as many characters as reasonably possible.
const FORCE_ASSOC
If set, treat JSON objects '{...}' as associative arrays.
const TRY_FIXING
If set, attempt to fix invalid JSON.
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
const XMLMETA_OK
Skip escaping the characters '<', '>', and '&', which have special meanings in HTML and XML.
static stripComments( $json)
Remove multiline and single line comments from an otherwise valid JSON input string.
const STRIP_COMMENTS
If set, strip comments from input before parsing as JSON.
static parse( $value, $options=0)
Decodes a JSON string.
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Generic operation result class Has warning/error list, boolean status and arbitrary value.