From 109f4ffd5e931382455170883c6724f21cfcfd4e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 4 Oct 2014 17:00:12 +0100 Subject: [PATCH] Normalize json across all php versions, fixes #3226 --- src/Composer/Json/JsonFile.php | 10 +++++++++- src/Composer/Json/JsonFormatter.php | 2 +- tests/Composer/Test/Json/JsonFileTest.php | 13 +------------ 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 2d35b9671..80718348c 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -184,7 +184,15 @@ class JsonFile public static function encode($data, $options = 448) { if (version_compare(PHP_VERSION, '5.4', '>=')) { - return json_encode($data, $options); + $json = json_encode($data, $options); + + // compact brackets to follow recent php versions + if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512)) { + $json = preg_replace('/\[\s+\]/', '[]', $json); + $json = preg_replace('/\{\s+\}/', '{}', $json); + } + + return $json; } $json = json_encode($data); diff --git a/src/Composer/Json/JsonFormatter.php b/src/Composer/Json/JsonFormatter.php index 614683608..025a53950 100644 --- a/src/Composer/Json/JsonFormatter.php +++ b/src/Composer/Json/JsonFormatter.php @@ -102,7 +102,7 @@ class JsonFormatter } } else { // Collapse empty {} and [] - $result = rtrim($result)."\n\n".$indentStr; + $result = rtrim($result); } } diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index 6566e4cc4..cf89da35e 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -131,21 +131,10 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase public function testFormatEmptyArray() { $data = array('test' => array(), 'test2' => new \stdClass); - if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512)) { - $json = '{ - "test": [ - - ], - "test2": { - - } -}'; - } else { - $json = '{ + $json = '{ "test": [], "test2": {} }'; - } $this->assertJsonFormat($json, $data); }