From 1a9de17a88576702c1403758f72b119e80848217 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 2 Oct 2011 21:14:00 +0200 Subject: [PATCH] Detect and report common mistakes in json syntax, refs #36 --- src/Composer/Json/JsonFile.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index d1396ca7d..060ce25de 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -107,6 +107,11 @@ class JsonFile break; case JSON_ERROR_SYNTAX: $msg = 'Syntax error'; + 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); + } 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); + } break; case JSON_ERROR_UTF8: $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';