commit
bca917db49
|
@ -19,6 +19,7 @@ use Composer\Composer;
|
||||||
* Reads/writes json files.
|
* Reads/writes json files.
|
||||||
*
|
*
|
||||||
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
|
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
class JsonFile
|
class JsonFile
|
||||||
{
|
{
|
||||||
|
@ -120,17 +121,29 @@ class JsonFile
|
||||||
break;
|
break;
|
||||||
case JSON_ERROR_SYNTAX:
|
case JSON_ERROR_SYNTAX:
|
||||||
$msg = 'Syntax error';
|
$msg = 'Syntax error';
|
||||||
|
$charOffset = 0;
|
||||||
if (preg_match('#["}\]]\s*(,)\s*\}#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
if (preg_match('#["}\]]\s*(,)\s*\}#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
||||||
$msg .= ', extra comma on line '.(substr_count(substr($json, 0, $match[1][1]), "\n")+1);
|
$msg .= ', extra comma';
|
||||||
|
} elseif (preg_match('#(["}\]]) *\r?\n *"#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
||||||
|
$msg .= ', missing comma';
|
||||||
|
$charOffset = 1;
|
||||||
|
} elseif (preg_match('#^ *([a-z0-9_-]+) *:#mi', $json, $match, PREG_OFFSET_CAPTURE)) {
|
||||||
|
$msg .= ', you must use double quotes (") around keys';
|
||||||
} elseif (preg_match('#(\'.+?\' *:|: *\'.+?\')#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
} elseif (preg_match('#(\'.+?\' *:|: *\'.+?\')#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
||||||
$msg .= ', use double quotes (") instead of single quotes (\') on line '.(substr_count(substr($json, 0, $match[1][1]), "\n")+1);
|
$msg .= ', use double quotes (") instead of single quotes (\')';
|
||||||
|
} elseif (preg_match('#(\[".*?":.*?\])#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
||||||
|
$msg .= ', you must use the hash syntax (e.g. {"foo": "bar"}) instead of array syntax (e.g. ["foo", "bar"])';
|
||||||
|
}
|
||||||
|
if (isset($match[1][1])) {
|
||||||
|
$preError = substr($json, 0, $match[1][1]);
|
||||||
|
$msg .= ' on line '.(substr_count($preError, "\n")+1).', char '.abs(strrpos($preError, "\n") - strlen($preError) - $charOffset);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case JSON_ERROR_UTF8:
|
case JSON_ERROR_UTF8:
|
||||||
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
throw new \UnexpectedValueException('Incorrect composer.json file: '.$msg);
|
throw new \UnexpectedValueException('JSON Parse Error: '.$msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Composer.
|
||||||
|
*
|
||||||
|
* (c) Nils Adermann <naderman@naderman.de>
|
||||||
|
* Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Composer\Test\Json;
|
||||||
|
|
||||||
|
use Composer\Json\JsonFile;
|
||||||
|
|
||||||
|
class JsonFileTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testParseErrorDetectExtraComma()
|
||||||
|
{
|
||||||
|
$json = '{
|
||||||
|
"foo": "bar",
|
||||||
|
}';
|
||||||
|
$this->expectParseException('extra comma on line 2, char 21', $json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParseErrorDetectSingleQuotes()
|
||||||
|
{
|
||||||
|
$json = '{
|
||||||
|
\'foo\': "bar"
|
||||||
|
}';
|
||||||
|
$this->expectParseException('use double quotes (") instead of single quotes (\') on line 2, char 9', $json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParseErrorDetectMissingQuotes()
|
||||||
|
{
|
||||||
|
$json = '{
|
||||||
|
foo: "bar"
|
||||||
|
}';
|
||||||
|
$this->expectParseException('must use double quotes (") around keys on line 2, char 9', $json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParseErrorDetectArrayAsHash()
|
||||||
|
{
|
||||||
|
$json = '{
|
||||||
|
"foo": ["bar": "baz"]
|
||||||
|
}';
|
||||||
|
$this->expectParseException('you must use the hash syntax (e.g. {"foo": "bar"}) instead of array syntax (e.g. ["foo", "bar"]) on line 2, char 16', $json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParseErrorDetectMissingComma()
|
||||||
|
{
|
||||||
|
$json = '{
|
||||||
|
"foo": "bar"
|
||||||
|
"bar": "foo"
|
||||||
|
}';
|
||||||
|
$this->expectParseException('missing comma on line 2, char 21', $json);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function expectParseException($text, $json)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
JsonFile::parseJson($json);
|
||||||
|
$this->fail();
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
$this->assertContains($text, $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue