* (cf. FormatJson::XMLMETA_OK). Use Xml::encodeJsVar() instead in such cases.
*
* @param mixed $value The value to encode. Can be any type except a resource.
- * @param bool $pretty If true, add non-significant whitespace to improve readability.
+ * @param 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).
* @param int $escaping Bitfield consisting of _OK class constants
* @return string|bool: String if successful; false upon failure
*/
public static function encode( $value, $pretty = false, $escaping = 0 ) {
+ if ( !is_string( $pretty ) ) {
+ $pretty = $pretty ? ' ' : false;
+ }
+
if ( defined( 'JSON_UNESCAPED_UNICODE' ) ) {
return self::encode54( $value, $pretty, $escaping );
}
* JSON encoder wrapper for PHP >= 5.4, which supports useful encoding options.
*
* @param mixed $value
- * @param bool $pretty
+ * @param string|bool $pretty
* @param int $escaping
* @return string|bool
*/
// which is hardly useful when '<' and '>' are escaped (and inadequate), and such
// escaping negatively impacts the human readability of URLs and similar strings.
$options = JSON_UNESCAPED_SLASHES;
- $options |= $pretty ? JSON_PRETTY_PRINT : 0;
+ $options |= $pretty !== false ? JSON_PRETTY_PRINT : 0;
$options |= ( $escaping & self::UTF8_OK ) ? JSON_UNESCAPED_UNICODE : 0;
$options |= ( $escaping & self::XMLMETA_OK ) ? 0 : ( JSON_HEX_TAG | JSON_HEX_AMP );
$json = json_encode( $value, $options );
return false;
}
- if ( $pretty ) {
+ if ( $pretty !== false ) {
// Remove whitespace inside empty arrays/objects; different JSON encoders
// vary on this, and we want our output to be consistent across implementations.
$json = preg_replace( self::WS_CLEANUP_REGEX, '', $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 ( $escaping & self::UTF8_OK ) {
$json = str_replace( self::$badChars, self::$badCharsEscaped, $json );
* Therefore, the missing options are implemented here purely in PHP code.
*
* @param mixed $value
- * @param bool $pretty
+ * @param string|bool $pretty
* @param int $escaping
* @return string|bool
*/
$json = str_replace( self::$badChars, self::$badCharsEscaped, $json );
}
- if ( $pretty ) {
- return self::prettyPrint( $json );
+ if ( $pretty !== false ) {
+ return self::prettyPrint( $json, $pretty );
}
return $json;
* Only needed for PHP < 5.4, which lacks the JSON_PRETTY_PRINT option.
*
* @param string $json
+ * @param string $indentString
* @return string
*/
- private static function prettyPrint( $json ) {
+ private static function prettyPrint( $json, $indentString ) {
$buf = '';
$indent = 0;
$json = strtr( $json, array( '\\\\' => '\\\\', '\"' => "\x01" ) );
++$indent;
// falls through
case ',':
- $buf .= $json[$i] . "\n" . str_repeat( ' ', $indent );
+ $buf .= $json[$i] . "\n" . str_repeat( $indentString, $indent );
break;
case ']':
case '}':
- $buf .= "\n" . str_repeat( ' ', --$indent ) . $json[$i];
+ $buf .= "\n" . str_repeat( $indentString, --$indent ) . $json[$i];
break;
case '"':
$skip = strcspn( $json, '"', $i + 1 ) + 2;
*/
class FormatJsonTest extends MediaWikiTestCase {
- public function testEncoderPrettyPrinting() {
+ public static function provideEncoderPrettyPrinting() {
+ return array(
+ // Four spaces
+ array( true, ' ' ),
+ array( ' ', ' ' ),
+ // Two spaces
+ array( ' ', ' ' ),
+ // One tab
+ array( "\t", "\t" ),
+ );
+ }
+
+ /**
+ * @dataProvider provideEncoderPrettyPrinting
+ */
+ public function testEncoderPrettyPrinting( $pretty, $expectedIndent ) {
$obj = array(
'emptyObject' => new stdClass,
'emptyArray' => array(),
),
);
- // 4 space indent, no trailing whitespace, no trailing linefeed
+ // No trailing whitespace, no trailing linefeed
$json = '{
- "emptyObject": {},
- "emptyArray": [],
- "string": "foobar\\\\",
- "filledArray": [
- [
- 123,
- 456
- ],
- "\"7\":[\"8\",{\"9\":\"10\"}]",
- "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}"
- ]
+ "emptyObject": {},
+ "emptyArray": [],
+ "string": "foobar\\\\",
+ "filledArray": [
+ [
+ 123,
+ 456
+ ],
+ "\"7\":[\"8\",{\"9\":\"10\"}]",
+ "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}"
+ ]
}';
$json = str_replace( "\r", '', $json ); // Windows compat
- $this->assertSame( $json, FormatJson::encode( $obj, true ) );
+ $json = str_replace( "\t", $expectedIndent, $json );
+ $this->assertSame( $json, FormatJson::encode( $obj, $pretty ) );
}
public static function provideEncodeDefault() {