Merge pull request #415 from hason/json
Removed definition of global constants reserved for PHP >=5.4 and fixed ...pull/416/merge
commit
a208d6cf43
|
@ -18,16 +18,6 @@ use JsonSchema\Validator;
|
|||
use Seld\JsonLint\JsonParser;
|
||||
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.
|
||||
*
|
||||
|
@ -39,6 +29,10 @@ class JsonFile
|
|||
const LAX_SCHEMA = 1;
|
||||
const STRICT_SCHEMA = 2;
|
||||
|
||||
const JSON_UNESCAPED_SLASHES = 64;
|
||||
const JSON_PRETTY_PRINT = 128;
|
||||
const JSON_UNESCAPED_UNICODE = 256;
|
||||
|
||||
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);
|
||||
|
||||
$prettyPrint = (Boolean) ($options & JSON_PRETTY_PRINT);
|
||||
$unescapeUnicode = (Boolean) ($options & JSON_UNESCAPED_UNICODE);
|
||||
$unescapeSlashes = (Boolean) ($options & JSON_UNESCAPED_SLASHES);
|
||||
$prettyPrint = (Boolean) ($options & self::JSON_PRETTY_PRINT);
|
||||
$unescapeUnicode = (Boolean) ($options & self::JSON_UNESCAPED_UNICODE);
|
||||
$unescapeSlashes = (Boolean) ($options & self::JSON_UNESCAPED_SLASHES);
|
||||
|
||||
if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes) {
|
||||
return $json;
|
||||
|
|
|
@ -140,9 +140,10 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
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');
|
||||
}
|
||||
|
||||
$data = array("Žluťoučký \" kůň" => "úpěl ďábelské ódy za €");
|
||||
$json = '{
|
||||
"Žluťoučký \" kůň": "úpěl ďábelské ódy za €"
|
||||
|
@ -151,14 +152,23 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
|
|||
$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');
|
||||
}
|
||||
$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()
|
||||
|
|
Loading…
Reference in New Issue