1
0
Fork 0

Merge pull request #307 from hason/slashes

Added support for JSON_UNESCAPED_SLASHES
pull/305/merge
Jordi Boggiano 2012-02-17 01:26:06 -08:00
commit deaaf2ef37
2 changed files with 40 additions and 18 deletions

View File

@ -113,22 +113,23 @@ class JsonFile
* Original code for this function can be found at: * Original code for this function can be found at:
* http://recursive-design.com/blog/2008/03/11/format-json-with-php/ * http://recursive-design.com/blog/2008/03/11/format-json-with-php/
* *
* @param array $hash Data to encode into a formatted JSON string * @param mixed $data Data to encode into a formatted JSON string
* @param int $options json_encode options * @param int $options json_encode options
* @return string Encoded json * @return string Encoded json
*/ */
static public function encode(array $hash, $options = 448) static public function encode($data, $options = 448)
{ {
if (version_compare(PHP_VERSION, '5.4', '>=')) { if (version_compare(PHP_VERSION, '5.4', '>=')) {
return json_encode($hash, $options); return json_encode($data, $options);
} }
$json = json_encode($hash); $json = json_encode($data);
$prettyPrint = (Boolean) ($options & JSON_PRETTY_PRINT); $prettyPrint = (Boolean) ($options & JSON_PRETTY_PRINT);
$unescapeUnicode = (Boolean) ($options & JSON_UNESCAPED_UNICODE); $unescapeUnicode = (Boolean) ($options & JSON_UNESCAPED_UNICODE);
$unescapeSlashes = (Boolean) ($options & JSON_UNESCAPED_SLASHES);
if (!$prettyPrint && !$unescapeUnicode) { if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes) {
return $json; return $json;
} }
@ -155,15 +156,18 @@ class JsonFile
$noescape = '\\' === $char ? !$noescape : true; $noescape = '\\' === $char ? !$noescape : true;
continue; continue;
} elseif ('' !== $buffer) { } elseif ('' !== $buffer) {
if ($unescapeUnicode && function_exists('mb_convert_encoding')) { if ($unescapeSlashes) {
// http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha $buffer = str_replace('\\/', '/', $buffer);
$result .= preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $buffer.$char);
} else {
$result .= $buffer.$char;
} }
if ($unescapeUnicode && function_exists('mb_convert_encoding')) {
// http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
$buffer = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $buffer);
}
$result .= $buffer.$char;
$buffer = ''; $buffer = '';
continue; continue;
} }
@ -213,7 +217,7 @@ class JsonFile
* *
* @param string $json json string * @param string $json json string
* *
* @return array * @return mixed
*/ */
static public function parseJson($json) static public function parseJson($json)
{ {

View File

@ -107,7 +107,7 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
{ {
$data = array('name' => 'composer/composer'); $data = array('name' => 'composer/composer');
$json = '{ $json = '{
"name": "composer\/composer" "name": "composer/composer"
}'; }';
$this->assertJsonFormat($json, $data); $this->assertJsonFormat($json, $data);
} }
@ -116,7 +116,7 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
{ {
$data = array('Metadata\\' => 'src/'); $data = array('Metadata\\' => 'src/');
$json = '{ $json = '{
"Metadata\\\\": "src\/" "Metadata\\\\": "src/"
}'; }';
$this->assertJsonFormat($json, $data); $this->assertJsonFormat($json, $data);
} }
@ -125,7 +125,7 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
{ {
$data = array("Metadata\\\"" => 'src/'); $data = array("Metadata\\\"" => 'src/');
$json = '{ $json = '{
"Metadata\\\\\\"": "src\/" "Metadata\\\\\\"": "src/"
}'; }';
$this->assertJsonFormat($json, $data); $this->assertJsonFormat($json, $data);
@ -141,6 +141,20 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
$this->assertJsonFormat($json, $data); $this->assertJsonFormat($json, $data);
} }
public function testEscapedSlashes()
{
$data = "\\/fooƌ";
$this->assertJsonFormat('"\\\\\\/fooƌ"', $data, JSON_UNESCAPED_UNICODE);
}
public function testEscapedUnicode()
{
$data = "ƌ";
$this->assertJsonFormat('"\\u018c"', $data, 0);
}
private function expectParseException($text, $json) private function expectParseException($text, $json)
{ {
try { try {
@ -151,11 +165,15 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
} }
} }
private function assertJsonFormat($json, $data) private function assertJsonFormat($json, $data, $options = null)
{ {
$file = new JsonFile('composer.json'); $file = new JsonFile('composer.json');
if (null === $options) {
$this->assertEquals($json, $file->encode($data)); $this->assertEquals($json, $file->encode($data));
} else {
$this->assertEquals($json, $file->encode($data, $options));
}
} }
} }