MediaWiki
1.28.0
|
JSON formatter wrapper class. More...
Static Public Member Functions | |
static | decode ($value, $assoc=false) |
Decodes a JSON string. More... | |
static | encode ($value, $pretty=false, $escaping=0) |
Returns the JSON representation of a value. More... | |
static | parse ($value, $options=0) |
Decodes a JSON string. More... | |
static | stripComments ($json) |
Remove multiline and single line comments from an otherwise valid JSON input string. More... | |
Public Attributes | |
const | ALL_OK = 3 |
Skip escaping as many characters as reasonably possible. More... | |
const | FORCE_ASSOC = 0x100 |
If set, treat json objects '{...}' as associative arrays. More... | |
const | STRIP_COMMENTS = 0x400 |
If set, strip comments from input before parsing as JSON. More... | |
const | TRY_FIXING = 0x200 |
If set, attempts to fix invalid json. More... | |
const | UTF8_OK = 1 |
Skip escaping most characters above U+007F for readability and compactness. More... | |
const | WS_CLEANUP_REGEX = '/(?<=[\[{])\n\s*+(?=[\]}])/' |
Regex that matches whitespace inside empty arrays and objects. More... | |
const | XMLMETA_OK = 2 |
Skip escaping the characters '<', '>', and '&', which have special meanings in HTML and XML. More... | |
Static Private Attributes | |
static | $badChars |
Characters problematic in JavaScript. More... | |
static | $badCharsEscaped |
Escape sequences for characters listed in FormatJson::$badChars. More... | |
JSON formatter wrapper class.
Definition at line 26 of file FormatJson.php.
|
static |
Decodes a JSON string.
It is recommended to use FormatJson::parse(), which returns more comprehensive result in case of an error, and has more parsing options.
string | $value | The JSON string being decoded |
bool | $assoc | When true, returned objects will be converted into associative arrays. |
null
is returned if $value represented null
, if $value could not be decoded, or if the encoded data was deeper than the recursion limit. Use FormatJson::parse() to distinguish between types of null
and to get proper error code. Definition at line 187 of file FormatJson.php.
References $value.
Referenced by BotPassword\__construct(), GitInfo\__construct(), ParserTestRunner\cleanupOption(), ForeignAPIRepo\fetchImageQuery(), ImportSiteScripts\fetchScriptList(), AutoloadGenerator\generateJsonAutoload(), ResourceLoaderModule\getFileDependencies(), Language\getGrammarTransformations(), FindHooks\getHooksFromOnlineDocCategory(), ForeignAPIRepo\getInfo(), JsonContent\getJsonData(), MWRestrictions\newFromJson(), MediaWiki\Site\MediaWikiPageNameNormalizer\normalizePageName(), SwiftFileBackend\objectListing(), ResourceLoader\preloadModuleInfo(), LocalisationCache\readJSONFile(), FormatJsonTest\testDecodeReturnType(), HtmlAutoCompleteSelectFieldTest\testGetAttributes(), and ExtensionProcessorTest\testGlobalSettingsDocumentedInSchema().
|
static |
Returns the JSON representation of a value.
mixed | $value | The value to encode. Can be any type except a resource. |
string | bool | $pretty | If a string, add non-significant whitespace to improve readability, using that string for indentation. If true, use the default indent string (four spaces). |
int | $escaping | Bitfield consisting of _OK class constants |
Definition at line 127 of file FormatJson.php.
References $options, and $value.
Referenced by JsonContent\beautifyJSON(), ApiFormatBase\closePrinter(), FileBackendMultiWrite\doOperationsInternal(), FileBackendStore\doOperationsInternal(), Xml\encodeJsVar(), WebInstallerName\execute(), ApiExpandTemplates\execute(), ListVariants\execute(), SpecialRunJobs\execute(), ApiParse\execute(), RunJobs\execute(), ApiFormatJson\execute(), ConvertExtensionToRegistration\execute(), GetConfiguration\execute(), JSONRCFeedFormatter\formatArray(), AutoloadGenerator\generateJsonAutoload(), MessageBlobStore\generateMessageBlob(), HTMLAutoCompleteSelectField\getAttributes(), HTMLTitleTextField\getDataAttribs(), HTMLFormField\getDiv(), HTMLFormField\getHelpTextHtmlDiv(), HTMLFormField\getHelpTextHtmlTable(), ResourceLoaderStartUpModule\getScript(), HTMLCheckMatrix\getTableRow(), HTMLFormField\getTableRow(), MWExceptionHandler\jsonSerializeException(), FileOp\logFailure(), ResourceLoader\makeModuleResponse(), BotPassword\newUnsaved(), SwiftFileBackend\onError(), Pingback\postPingback(), GitInfo\precomputeValues(), JsonContent\primitiveValue(), FileBackendMultiWrite\resyncFiles(), BotPassword\save(), ResourceLoaderModule\saveFileDependencies(), FormatJsonTest\testEncodeAllOk(), FormatJsonTest\testEncodeDefault(), FormatJsonTest\testEncodePhpBug46944(), FormatJsonTest\testEncoderPrettyPrinting(), FormatJsonTest\testEncodeUtf8(), FormatJsonTest\testEncodeXmlMeta(), JsonContentTest\testFillParserOutput(), MWRestrictionsTest\testJson(), FormatJsonTest\testParse(), FormatJsonTest\testParseTryFixing(), MWRestrictions\toJson(), Job\toString(), GenerateJsonI18n\transformI18nFile(), and GenerateJqueryMsgData\writeJavascriptFile().
|
static |
Decodes a JSON string.
Unlike FormatJson::decode(), if $value represents null value, it will be properly decoded as valid.
string | $value | The JSON string being decoded |
int | $options | A bit field that allows FORCE_ASSOC, TRY_FIXING, STRIP_COMMENTS |
Definition at line 201 of file FormatJson.php.
References $code, $count, $options, $value, StatusValue\newFatal(), StatusValue\newGood(), and wfMessage().
Referenced by JsonContent\getData(), ApiCSPReport\getReport(), FormatJsonTest\testParse(), FormatJsonTest\testParseErrors(), FormatJsonTest\testParseStripComments(), and FormatJsonTest\testParseTryFixing().
|
static |
Remove multiline and single line comments from an otherwise valid JSON input string.
This can be used as a preprocessor for to allow JSON formatted configuration files to contain comments.
string | $json |
Definition at line 272 of file FormatJson.php.
References $buffer, and string.
Referenced by FormatJsonTest\testStripComments().
|
staticprivate |
Characters problematic in JavaScript.
Definition at line 97 of file FormatJson.php.
|
staticprivate |
Escape sequences for characters listed in FormatJson::$badChars.
Definition at line 105 of file FormatJson.php.
const FormatJson::ALL_OK = 3 |
Skip escaping as many characters as reasonably possible.
Definition at line 55 of file FormatJson.php.
Referenced by ApiFormatBase\closePrinter(), ApiExpandTemplates\execute(), ApiParse\execute(), ApiFormatJson\execute(), ConvertExtensionToRegistration\execute(), ResourceLoaderStartUpModule\getScript(), MWExceptionHandler\logError(), MWExceptionHandler\logException(), FormatJsonTest\testEncodeAllOk(), MWRestrictionsTest\testJson(), FormatJsonTest\testParse(), FormatJsonTest\testParseTryFixing(), MWRestrictions\toJson(), and GenerateJsonI18n\transformI18nFile().
const FormatJson::FORCE_ASSOC = 0x100 |
If set, treat json objects '{...}' as associative arrays.
Without this option, json objects will be converted to stdClass. The value is set to 1 to be backward compatible with 'true' that was used before.
Definition at line 64 of file FormatJson.php.
Referenced by ApiCSPReport\getReport(), and FormatJsonTest\testParse().
const FormatJson::STRIP_COMMENTS = 0x400 |
If set, strip comments from input before parsing as JSON.
Definition at line 78 of file FormatJson.php.
Referenced by FormatJsonTest\testParseStripComments().
const FormatJson::TRY_FIXING = 0x200 |
If set, attempts to fix invalid json.
Definition at line 71 of file FormatJson.php.
Referenced by FormatJsonTest\testParseTryFixing().
const FormatJson::UTF8_OK = 1 |
Skip escaping most characters above U+007F for readability and compactness.
This encoding option saves 3 to 8 bytes (uncompressed) for each such character; however, it could break compatibility with systems that incorrectly handle UTF-8.
Definition at line 34 of file FormatJson.php.
Referenced by JsonContent\beautifyJSON(), Xml\encodeJsVar(), and FormatJsonTest\testEncodeUtf8().
const FormatJson::WS_CLEANUP_REGEX = '/(?<=[\[{])\n\s*+(?=[\]}])/' |
Regex that matches whitespace inside empty arrays and objects.
This doesn't affect regular strings inside the JSON because those can't have a real line break (\n) in them, at this point they are already escaped as the string "\\n" which this doesn't match.
Definition at line 89 of file FormatJson.php.
const FormatJson::XMLMETA_OK = 2 |
Skip escaping the characters '<', '>', and '&', which have special meanings in HTML and XML.
Definition at line 46 of file FormatJson.php.
Referenced by ApiFormatJson\execute(), and FormatJsonTest\testEncodeXmlMeta().