diff --git a/src/Composer/Json/JsonFormatter.php b/src/Composer/Json/JsonFormatter.php index a7f3b2ba0..f06928e34 100644 --- a/src/Composer/Json/JsonFormatter.php +++ b/src/Composer/Json/JsonFormatter.php @@ -69,9 +69,13 @@ class JsonFormatter $buffer = preg_replace_callback('/(\\\\+)u([0-9a-f]{4})/i', function($match) { $l = strlen($match[1]); - if ($l%2) - return str_repeat ('\\', $l-1).mb_convert_encoding(pack('H*', $match[2]), 'UTF-8', 'UCS-2BE'); - + if ($l % 2) { + return str_repeat('\\', $l - 1) . mb_convert_encoding( + pack('H*', $match[2]), + 'UTF-8', + 'UCS-2BE' + ); + } return $match[0]; }, $buffer); } diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index 79a1e40f4..4c96720da 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -198,6 +198,18 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase $this->assertJsonFormat('"\\u018c"', $data, 0); } + public function testDoubleEscapedUnicode() + { + $jsonFile = new JsonFile('composer.json'); + $data = array("Zdjęcia","hjkjhl\\u0119kkjk"); + $encodedData = $jsonFile->encode($data); + $doubleEncodedData = $jsonFile->encode(array('t' => $encodedData)); + + $decodedData = json_decode($doubleEncodedData, true); + $doubleData = json_decode($decodedData['t'], true); + $this->assertEquals($data, $doubleData); + } + private function expectParseException($text, $json) { try {