1
0
Fork 0

Normalize json across all php versions, fixes #3226

pull/3328/head
Jordi Boggiano 2014-10-04 17:00:12 +01:00
parent 3d1a094535
commit 109f4ffd5e
3 changed files with 11 additions and 14 deletions

View File

@ -184,7 +184,15 @@ class JsonFile
public static function encode($data, $options = 448) public static function encode($data, $options = 448)
{ {
if (version_compare(PHP_VERSION, '5.4', '>=')) { 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); $json = json_encode($data);

View File

@ -102,7 +102,7 @@ class JsonFormatter
} }
} else { } else {
// Collapse empty {} and [] // Collapse empty {} and []
$result = rtrim($result)."\n\n".$indentStr; $result = rtrim($result);
} }
} }

View File

@ -131,21 +131,10 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
public function testFormatEmptyArray() public function testFormatEmptyArray()
{ {
$data = array('test' => array(), 'test2' => new \stdClass); $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": [], "test": [],
"test2": {} "test2": {}
}'; }';
}
$this->assertJsonFormat($json, $data); $this->assertJsonFormat($json, $data);
} }