1
0
Fork 0

Merge pull request #415 from hason/json

Removed definition of global constants reserved for PHP >=5.4 and fixed ...
pull/416/merge
Jordi Boggiano 2012-03-09 00:42:45 -08:00
commit a208d6cf43
2 changed files with 23 additions and 19 deletions

View File

@ -18,16 +18,6 @@ use JsonSchema\Validator;
use Seld\JsonLint\JsonParser; use Seld\JsonLint\JsonParser;
use Composer\Util\StreamContextFactory; use Composer\Util\StreamContextFactory;
if (!defined('JSON_UNESCAPED_SLASHES')) {
define('JSON_UNESCAPED_SLASHES', 64);
}
if (!defined('JSON_PRETTY_PRINT')) {
define('JSON_PRETTY_PRINT', 128);
}
if (!defined('JSON_UNESCAPED_UNICODE')) {
define('JSON_UNESCAPED_UNICODE', 256);
}
/** /**
* Reads/writes json files. * Reads/writes json files.
* *
@ -39,6 +29,10 @@ class JsonFile
const LAX_SCHEMA = 1; const LAX_SCHEMA = 1;
const STRICT_SCHEMA = 2; const STRICT_SCHEMA = 2;
const JSON_UNESCAPED_SLASHES = 64;
const JSON_PRETTY_PRINT = 128;
const JSON_UNESCAPED_UNICODE = 256;
private $path; private $path;
/** /**
@ -108,7 +102,7 @@ class JsonFile
); );
} }
} }
file_put_contents($this->path, static::encode($hash, $options). ($options & JSON_PRETTY_PRINT ? "\n" : '')); file_put_contents($this->path, static::encode($hash, $options). ($options & self::JSON_PRETTY_PRINT ? "\n" : ''));
} }
/** /**
@ -170,9 +164,9 @@ class JsonFile
$json = json_encode($data); $json = json_encode($data);
$prettyPrint = (Boolean) ($options & JSON_PRETTY_PRINT); $prettyPrint = (Boolean) ($options & self::JSON_PRETTY_PRINT);
$unescapeUnicode = (Boolean) ($options & JSON_UNESCAPED_UNICODE); $unescapeUnicode = (Boolean) ($options & self::JSON_UNESCAPED_UNICODE);
$unescapeSlashes = (Boolean) ($options & JSON_UNESCAPED_SLASHES); $unescapeSlashes = (Boolean) ($options & self::JSON_UNESCAPED_SLASHES);
if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes) { if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes) {
return $json; return $json;

View File

@ -140,9 +140,10 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
public function testUnicode() public function testUnicode()
{ {
if (!function_exists('mb_convert_encoding')) { if (!function_exists('mb_convert_encoding') && version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped('Test requires the mbstring extension'); $this->markTestSkipped('Test requires the mbstring extension');
} }
$data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €"); $data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
$json = '{ $json = '{
"Žluťoučký \" kůň": "úpěl ďábelské ódy za €" "Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
@ -151,14 +152,23 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
$this->assertJsonFormat($json, $data); $this->assertJsonFormat($json, $data);
} }
public function testEscapedSlashes() public function testOnlyUnicode()
{ {
if (!function_exists('mb_convert_encoding')) { if (!function_exists('mb_convert_encoding') && version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped('Test requires the mbstring extension'); $this->markTestSkipped('Test requires the mbstring extension');
} }
$data = "\\/fooƌ";
$this->assertJsonFormat('"\\\\\\/fooƌ"', $data, JSON_UNESCAPED_UNICODE); $data = "\\";
$this->assertJsonFormat('"\\\\\\/ƌ"', $data, JsonFile::JSON_UNESCAPED_UNICODE);
}
public function testEscapedSlashes()
{
$data = "\\/foo";
$this->assertJsonFormat('"\\\\\\/foo"', $data, 0);
} }
public function testEscapedUnicode() public function testEscapedUnicode()