8025147: Trailing comma is not allowed in JSONArray and JSONObject

Fri, 20 Sep 2013 20:55:43 +0530

author
sundar
date
Fri, 20 Sep 2013 20:55:43 +0530
changeset 568
279f47b353f3
parent 566
fa491b75d3e4
child 569
16b6db9f7225

8025147: Trailing comma is not allowed in JSONArray and JSONObject
Reviewed-by: hannesw, jlaskey

src/jdk/nashorn/internal/parser/JSONParser.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/resources/Messages.properties file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025147.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025147.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/parser/JSONParser.java	Fri Sep 20 12:11:08 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/parser/JSONParser.java	Fri Sep 20 20:55:43 2013 +0530
     1.3 @@ -349,6 +349,10 @@
     1.4  
     1.5              case COMMARIGHT:
     1.6                  next();
     1.7 +                // check for trailing comma - not allowed in JSON
     1.8 +                if (type == RBRACKET) {
     1.9 +                    throw error(AbstractParser.message("trailing.comma.in.json", type.getNameOrType()));
    1.10 +                }
    1.11                  break;
    1.12  
    1.13              default:
    1.14 @@ -388,6 +392,10 @@
    1.15  
    1.16              case COMMARIGHT:
    1.17                  next();
    1.18 +                // check for trailing comma - not allowed in JSON
    1.19 +                if (type == RBRACE) {
    1.20 +                    throw error(AbstractParser.message("trailing.comma.in.json", type.getNameOrType()));
    1.21 +                }
    1.22                  break;
    1.23  
    1.24              default:
     2.1 --- a/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Fri Sep 20 12:11:08 2013 +0200
     2.2 +++ b/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Fri Sep 20 20:55:43 2013 +0530
     2.3 @@ -57,6 +57,7 @@
     2.4  parser.error.regex.unsupported.flag=Unsupported RegExp flag: {0}
     2.5  parser.error.regex.repeated.flag=Repeated RegExp flag: {0}
     2.6  parser.error.regex.syntax={0}
     2.7 +parser.error.trailing.comma.in.json=Trailing comma is not allowed in JSON
     2.8  
     2.9  # strict mode error messages
    2.10  parser.error.strict.no.with="with" statement cannot be used in strict mode
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8025147.js	Fri Sep 20 20:55:43 2013 +0530
     3.3 @@ -0,0 +1,41 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + * 
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + * 
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + * 
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + * 
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/**
    3.28 + * JDK-8025147: Trailing comma is not allowed in JSONArray and JSONObject
    3.29 + *
    3.30 + * @test
    3.31 + * @run
    3.32 + */
    3.33 +
    3.34 +function check(str) {
    3.35 +    try {
    3.36 +        JSON.parse(str);
    3.37 +        fail("should have thrown SyntaxError for " + str);
    3.38 +    } catch (e) {
    3.39 +        print(e);
    3.40 +    }
    3.41 +}
    3.42 +
    3.43 +check("{ \"a\": 333, }");
    3.44 +check("[ 4343, ]");
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/script/basic/JDK-8025147.js.EXPECTED	Fri Sep 20 20:55:43 2013 +0530
     4.3 @@ -0,0 +1,6 @@
     4.4 +SyntaxError: Invalid JSON: <json>:1:12 Trailing comma is not allowed in JSON
     4.5 +{ "a": 333, }
     4.6 +            ^
     4.7 +SyntaxError: Invalid JSON: <json>:1:8 Trailing comma is not allowed in JSON
     4.8 +[ 4343, ]
     4.9 +        ^

mercurial