Merge "FormatJson: Remove whitespace from empty arrays and objects"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 25 Oct 2013 22:33:12 +0000 (22:33 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 25 Oct 2013 22:33:12 +0000 (22:33 +0000)
includes/json/FormatJson.php
tests/phpunit/includes/json/FormatJsonTest.php

index bc2fff1..d611651 100644 (file)
@@ -55,6 +55,17 @@ class FormatJson {
         */
        const ALL_OK = 3;
 
+       /**
+        * 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.
         *
@@ -130,6 +141,12 @@ class FormatJson {
                if ( $json === false ) {
                        return false;
                }
+
+               if ( $pretty ) {
+                       // 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 ( $escaping & self::UTF8_OK ) {
                        $json = str_replace( self::$badChars, self::$badCharsEscaped, $json );
                }
@@ -213,6 +230,7 @@ class FormatJson {
                                        $buf .= substr( $json, $i, $skip );
                        }
                }
-               return str_replace( "\x01", '\"', preg_replace( '/ +$/m', '', $buf ) );
+               $buf = preg_replace( self::WS_CLEANUP_REGEX, '', $buf );
+               return str_replace( "\x01", '\"', $buf );
        }
 }
index 3eeaaed..8359f0d 100644 (file)
@@ -15,28 +15,30 @@ class FormatJsonTest extends MediaWikiTestCase {
                                        123,
                                        456,
                                ),
+                               // Nested json works without problems
                                '"7":["8",{"9":"10"}]',
+                               // Whitespace clean up doesn't touch strings that look alike
+                               "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}",
                        ),
                );
 
                // 4 space indent, no trailing whitespace, no trailing linefeed
                $json = '{
-    "emptyObject": {
-    },
-    "emptyArray": [
-    ],
+    "emptyObject": {},
+    "emptyArray": [],
     "string": "foobar\\\\",
     "filledArray": [
         [
             123,
             456
         ],
-        "\"7\":[\"8\",{\"9\":\"10\"}]"
+        "\"7\":[\"8\",{\"9\":\"10\"}]",
+        "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}"
     ]
 }';
 
                $json = str_replace( "\r", '', $json ); // Windows compat
-               $this->assertSame( $json, str_replace("\n\n", "\n", FormatJson::encode( $obj, true ) ));
+               $this->assertSame( $json, FormatJson::encode( $obj, true ) );
        }
 
        public static function provideEncodeDefault() {