abdulrehmanz / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

JsonParser.parse( String json) returns JsonNull instead of JsonSyntaxException #443

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Following code:
  String badJson = "[[]";
  JsonElement jsonElem = jsonParser.parse( badJson );

  if ( jsonElem.isJsonNull() ) {
    out.println("Request-String has not correct JSON-Format!");
    return;
  }

  out.println("Parsing successfull...");

What is the expected output? What do you see instead?
In my understanding of the documentation the function should throw a 
JsonSyntaxException. Confusingly the no catch block is required by Eclipse, 
although the method definition specifies a throw. Instead we get an instance of 
JsonNull...

What version of the product are you using? On what operating system?
Eclipse and Gson 2.1

Please provide any additional information below.
the error may be located in the JsonParser Class 
(http://google-gson.googlecode.com/svn/trunk/gson/src/main/java/com/google/gson/
JsonParser.java) in the JsonElement parse(Reader json) method

The if statement should check for if (element.isJsonNull() && jsonReader.peek() 
!= JsonToken.END_DOCUMENT)

and not if (!element.isJsonNull() && jsonReader.peek() != 
JsonToken.END_DOCUMENT)

    try {
      JsonReader jsonReader = new JsonReader(json);
      JsonElement element = parse(jsonReader);
      if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {
        throw new JsonSyntaxException("Did not consume the entire document.");
      }

Original issue reported on code.google.com by luscus.r...@gmail.com on 11 May 2012 at 2:39

GoogleCodeExporter commented 9 years ago
Please simply ignore this line: Confusingly the no catch block is required by 
Eclipse, although the method definition specifies a throw.

What is the expected output? What do you see instead?
In my understanding of the documentation the function should throw a 
JsonSyntaxException. Instead we get an instance of JsonNull...

Original comment by luscus.r...@gmail.com on 11 May 2012 at 2:58

GoogleCodeExporter commented 9 years ago
Good find. That's definitely bad behavior on Gson's part.

Your best bet is to avoid JsonParser. Instead just do this:
    new Gson().fromJson("[[]", JsonElement.class)

Inder, any recommendations on how we should proceed here? I suspect the current 
behavior is for backwards compatibility with Gson 1.6.

Original comment by limpbizkit on 30 Jun 2012 at 3:17

GoogleCodeExporter commented 9 years ago
I think we should fix JsonParser to throw a JsonSyntax exception.

Original comment by inder123 on 30 Jun 2012 at 6:23

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1156.

Original comment by inder123 on 30 Jun 2012 at 6:48

GoogleCodeExporter commented 9 years ago
Fixed in r1156 
However, it now throws JsonIOException instead of JsonParseException. It can be 
argued that JsonSyntaxException would have been a better choice. However, since 
the root cause is an IOException (EOFException), JsonIOException makes sense 
too.

Somewhat inconsistently, the same done view gson.fromJson("[[]", 
Object[].class) throws JsonSyntaxException.

IMHO, this incosistency is tolerable, though we can easily fix it by adding a 
check in JsonParser. I would like to hear other opinions before making that 
change though.

Original comment by inder123 on 30 Jun 2012 at 6:51

GoogleCodeExporter commented 9 years ago
Changed the behavior to throw JsonSyntaxException in r1157

Original comment by in...@trymph.com on 2 Jul 2012 at 6:37