1
0
Fork 0

Fixed detection of missing comma and added detection of missing colon in json

pull/256/head
Martin Hasoň 2012-01-24 15:10:55 +01:00
parent b9f0e89e7b
commit 76a2be5ae0
2 changed files with 22 additions and 1 deletions

View File

@ -206,7 +206,7 @@ class JsonFile
$msg .= ', extra comma';
} elseif (preg_match('#((?<=[^\\\\])\\\\(?!["\\\\/bfnrt]|u[a-f0-9]{4}))#i', $json, $match, PREG_OFFSET_CAPTURE)) {
$msg .= ', unescaped backslash (\\)';
} elseif (preg_match('#(["}\]]) *\r?\n *"#', $json, $match, PREG_OFFSET_CAPTURE)) {
} 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)) {
@ -215,6 +215,8 @@ class JsonFile
$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"])';
} elseif (preg_match('#".*?"( *["{\[])#', $json, $match, PREG_OFFSET_CAPTURE)) {
$msg .= ', missing colon';
}
if (isset($match[1][1])) {
$preError = substr($json, 0, $match[1][1]);

View File

@ -84,6 +84,25 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
$this->expectParseException('missing comma on line 2, char 21', $json);
}
public function testParseErrorDetectMissingCommaMultiline()
{
$json = '{
"foo": "barbar"
"bar": "foo"
}';
$this->expectParseException('missing comma on line 2, char 24', $json);
}
public function testParseErrorDetectMissingColon()
{
$json = '{
"foo": "bar",
"bar" "foo"
}';
$this->expectParseException('missing colon on line 3, char 14', $json);
}
public function testSimpleJsonString()
{
$data = array('name' => 'composer/composer');