From f5aa3e6c79648016daca134f71f59bd5c81ad5a4 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 1 Nov 2011 14:13:22 +0100 Subject: [PATCH] Add detection for extra commas in arrays --- src/Composer/Json/JsonFile.php | 2 +- tests/Composer/Test/Json/JsonFileTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index cba1e28de..60c601175 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -122,7 +122,7 @@ class JsonFile case JSON_ERROR_SYNTAX: $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'; } elseif (preg_match('#(["}\]]) *\r?\n *"#', $json, $match, PREG_OFFSET_CAPTURE)) { $msg .= ', missing comma'; diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index e25f86fdf..b1f46678a 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -24,6 +24,16 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase $this->expectParseException('extra comma on line 2, char 21', $json); } + public function testParseErrorDetectExtraCommaInArray() + { + $json = '{ + "foo": [ + "bar", + ] +}'; + $this->expectParseException('extra comma on line 3, char 18', $json); + } + public function testParseErrorDetectSingleQuotes() { $json = '{