* HTML and XML.
*
* @warning Do not use this option for JSON that could end up in inline scripts.
- * - HTML5, §4.3.1.2 Restrictions for contents of script elements
+ * - HTML 5.2, §4.12.1.3 Restrictions for contents of script elements
* - XML 1.0 (5th Ed.), §2.4 Character Data and Markup
*
* @since 1.22
*
* @since 1.22
*/
- const ALL_OK = 3;
+ const ALL_OK = self::UTF8_OK | self::XMLMETA_OK;
/**
- * 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.
+ * If set, treat JSON objects '{...}' as associative arrays. Without this option,
+ * JSON objects will be converted to stdClass.
*
* @since 1.24
*/
const FORCE_ASSOC = 0x100;
/**
- * If set, attempts to fix invalid json.
+ * If set, attempt to fix invalid JSON.
*
* @since 1.24
*/
*/
const STRIP_COMMENTS = 0x400;
- /**
- * 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.
- *
- * @private
- */
- const WS_CLEANUP_REGEX = '/(?<=[\[{])\n\s*+(?=[\]}])/';
-
/**
* Characters problematic in JavaScript.
*
* and U+000D (CR). However, PHP already escapes LF and CR according to RFC 4627.
*/
private static $badChars = [
- "\xe2\x80\xa8", // U+2028 LINE SEPARATOR
- "\xe2\x80\xa9", // U+2029 PARAGRAPH SEPARATOR
+ "\u{2028}", // U+2028 LINE SEPARATOR
+ "\u{2029}", // U+2029 PARAGRAPH SEPARATOR
];
/**
$pretty = $pretty ? ' ' : false;
}
- static $bug66021;
- if ( $pretty !== false && $bug66021 === null ) {
- $bug66021 = json_encode( [], JSON_PRETTY_PRINT ) !== '[]';
- }
-
// PHP escapes '/' to prevent breaking out of inline script blocks using '</script>',
// which is hardly useful when '<' and '>' are escaped (and inadequate), and such
// escaping negatively impacts the human readability of URLs and similar strings.
return false;
}
- if ( $pretty !== false ) {
- // Workaround for <https://bugs.php.net/bug.php?id=66021>
- if ( $bug66021 ) {
- $json = preg_replace( self::WS_CLEANUP_REGEX, '', $json );
+ if ( $pretty !== false && $pretty !== ' ' ) {
+ // Change the four-space indent to a tab indent
+ $json = str_replace( "\n ", "\n\t", $json );
+ while ( strpos( $json, "\t " ) !== false ) {
+ $json = str_replace( "\t ", "\t\t", $json );
}
- if ( $pretty !== ' ' ) {
- // Change the four-space indent to a tab indent
- $json = str_replace( "\n ", "\n\t", $json );
- while ( strpos( $json, "\t " ) !== false ) {
- $json = str_replace( "\t ", "\t\t", $json );
- }
- if ( $pretty !== "\t" ) {
- // Change the tab indent to the provided indent
- $json = str_replace( "\t", $pretty, $json );
- }
+ if ( $pretty !== "\t" ) {
+ // Change the tab indent to the provided indent
+ $json = str_replace( "\t", $pretty, $json );
}
}
if ( $escaping & self::UTF8_OK ) {
/**
* Remove multiline and single line comments from an otherwise valid JSON
- * input string. This can be used as a preprocessor for to allow JSON
+ * input string. This can be used as a preprocessor, to allow JSON
* formatted configuration files to contain comments.
*
* @param string $json