MediaWiki
1.34.4
|
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 = self::UTF8_OK | self::XMLMETA_OK |
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, attempt to fix invalid JSON. More... | |
const | UTF8_OK = 1 |
Skip escaping most characters above U+007F for readability and compactness. 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.
In PHP versions before 7.1, decoding a JSON string containing an empty key without passing $assoc as true results in a return object with a property named "_empty_" (because true empty properties were not supported pre-PHP-7.1). Instead, consider passing $assoc as true to return an associative array.
But be aware that in all supported PHP versions, decoding an empty JSON object with $assoc = true returns an array, not an object, breaking round-trip consistency.
See https://phabricator.wikimedia.org/T206411 for more details on these quirks.
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 174 of file FormatJson.php.
Referenced by BotPassword\__construct(), GitInfo\__construct(), ChangesListSpecialPage\considerActionsForDefaultSavedQuery(), UpdateExtensionJsonSchema\execute(), ApiCategoryTree\execute(), MediaWiki\Extension\OATHAuth\Api\Module\ApiOATHValidate\execute(), LocalisationUpdate\GitHubFetcher\fetchDirectory(), ForeignAPIRepo\fetchImageQuery(), ImportSiteScripts\fetchScriptList(), MediaWiki\Extension\OATHAuth\OATHUserRepository\findByUser(), AutoloadGenerator\generateJsonAutoload(), CommentStore\getCommentInternal(), Language\getGrammarTransformations(), FindHooks\getHooksFromOnlineDocCategory(), ForeignAPIRepo\getInfo(), MWRestrictions\newFromJson(), MediaWiki\Site\MediaWikiPageNameNormalizer\normalizePageName(), SwiftFileBackend\objectListing(), LocalisationUpdate\onRecacheFallback(), LocalisationUpdate\JSONReader\parse(), MediaWiki\Extensions\ConfirmEdit\hCaptcha\HCaptcha\passCaptcha(), ReCaptchaNoCaptcha\passCaptcha(), LocalisationCache\readJSONFile(), and MediaWiki\Extension\OATHAuth\Hook\LoadExtensionSchemaUpdates\UpdateTables\switchTOTPToMultipleKeys().
|
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 115 of file FormatJson.php.
References UTF8_OK, and XMLMETA_OK.
Referenced by GadgetDefinitionContent\beautifyJSON(), JsonContent\beautifyJSON(), MediaWiki\Extension\OATHAuth\OATHUserRepository\checkAndResolveLegacy(), ApiFormatBase\closePrinter(), MediaWiki\Extension\OATHAuth\Hook\LoadExtensionSchemaUpdates\UpdateTables\convertToGenericFields(), CommentStore\createComment(), FileBackendMultiWrite\doOperationsInternal(), FileBackendStore\doOperationsInternal(), Xml\encodeJsVar(), CategoryTree\encodeOptions(), UpdateExtensionJsonSchema\execute(), Update\execute(), WebInstallerName\execute(), ApiHelp\execute(), ApiExpandTemplates\execute(), SpecialRunJobs\execute(), ListVariants\execute(), ApiParse\execute(), ApiFormatJson\execute(), RunJobs\execute(), ConvertExtensionToRegistration\execute(), GetConfiguration\execute(), JSONRCFeedFormatter\formatArray(), AutoloadGenerator\generateJsonAutoload(), MessageBlobStore\generateMessageBlob(), HTMLAutoCompleteSelectField\getAttributes(), HTMLTitleTextField\getDataAttribs(), HTMLFormField\getDiv(), HTMLFormField\getHelpTextHtmlDiv(), HTMLFormField\getHelpTextHtmlTable(), CiteDataModule\getScript(), HTMLCheckMatrix\getTableRow(), HTMLFormField\getTableRow(), Scribunto_LuaTextLibrary\jsonEncode(), MWExceptionHandler\jsonSerializeException(), FileOp\logFailure(), makeCacheKeyHash(), GadgetDefinitionContentHandler\makeEmptyContent(), BotPassword\newUnsaved(), SwiftFileBackend\onError(), CiteHooks\onLinksUpdate(), MediaWiki\Extension\OATHAuth\OATHUserRepository\persist(), Pingback\postPingback(), GitInfo\precomputeValues(), JsonContent\primitiveValue(), FileBackendMultiWrite\resyncFiles(), BotPassword\save(), MediaWiki\Extension\OATHAuth\Hook\LoadExtensionSchemaUpdates\UpdateTables\switchTOTPToMultipleKeys(), MWRestrictions\toJson(), Job\toString(), and GenerateJsonI18n\transformI18nFile().
|
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 188 of file FormatJson.php.
References FORCE_ASSOC, StatusValue\newFatal(), StatusValue\newGood(), stripComments(), and wfMessage().
Referenced by JsonContent\getData(), ApiCSPReport\getReport(), and Scribunto_LuaTextLibrary\jsonDecode().
|
static |
Remove multiline and single line comments from an otherwise valid JSON input string.
This can be used as a preprocessor, to allow JSON formatted configuration files to contain comments.
string | $json |
Definition at line 259 of file FormatJson.php.
Referenced by parse().
|
staticprivate |
Characters problematic in JavaScript.
Definition at line 85 of file FormatJson.php.
|
staticprivate |
Escape sequences for characters listed in FormatJson::$badChars.
Definition at line 93 of file FormatJson.php.
const FormatJson::ALL_OK = self::UTF8_OK | self::XMLMETA_OK |
Skip escaping as many characters as reasonably possible.
Definition at line 55 of file FormatJson.php.
Referenced by ApiFormatBase\closePrinter(), CommentStore\createComment(), UpdateExtensionJsonSchema\execute(), ApiExpandTemplates\execute(), ApiParse\execute(), ApiFormatJson\execute(), ConvertExtensionToRegistration\execute(), AutoloadGenerator\generateJsonAutoload(), Scribunto_LuaTextLibrary\jsonEncode(), MWExceptionHandler\logError(), MWExceptionHandler\logException(), makeCacheKeyHash(), CiteHooks\onLinksUpdate(), 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.
Definition at line 63 of file FormatJson.php.
Referenced by ApiCSPReport\getReport(), Scribunto_LuaTextLibrary\jsonDecode(), and parse().
const FormatJson::STRIP_COMMENTS = 0x400 |
If set, strip comments from input before parsing as JSON.
Definition at line 77 of file FormatJson.php.
const FormatJson::TRY_FIXING = 0x200 |
If set, attempt to fix invalid JSON.
Definition at line 70 of file FormatJson.php.
Referenced by Scribunto_LuaTextLibrary\jsonDecode().
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 GadgetDefinitionContent\beautifyJSON(), JsonContent\beautifyJSON(), encode(), Xml\encodeJsVar(), ApiHelp\execute(), and MessageBlobStore\generateMessageBlob().
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 encode(), and ApiFormatJson\execute().