From 76a2be5ae07d49b64b3c4d62eb6e478344fb2a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 24 Jan 2012 15:10:55 +0100 Subject: [PATCH] Fixed detection of missing comma and added detection of missing colon in json --- src/Composer/Json/JsonFile.php | 4 +++- tests/Composer/Test/Json/JsonFileTest.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index fd43c9148..7594b85cc 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -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]); diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index 231839416..4ebf7b0c5 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -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');