AiorosXu / google-gson

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

JsonParser.jj wrong grammar #115

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Please take a look at the JsonParser.jj file in you source control.
Parse method looks like this:

public JsonElement parse() :
{
  JsonElement json = null;
}
{
  [")]}'\n"]( json=JsonObject() |
    json=JsonArray() |
    json=JsonPrimitive() |
    json=JsonNull())
  { return json; }
}

What is the expected output? What do you see instead?

I've found at least two instances of json text causing to return incorrect
result. Try to proceed parsing with the following input:

1. JsonElement element = new JsonParser(new StringReader("  ")).parse();

Expected element is to be null, but it failed with exception:

Encountered "<EOF>" at line 1, column 2.
Was expecting one of:
    <DIGITS> ...
    "null" ...
    "NaN" ...
    "Infinity" ...
    <BOOLEAN> ...
    <SINGLE_QUOTE_LITERAL> ...
    <DOUBLE_QUOTE_LITERAL> ...
    "[" ...
    "{" ...
    "-" ...
    ")]}\'\n" ...

2. Second option is event more harmful:
JsonElement element = new JsonParser(new StringReader("{}, 12, 23")).parse();

Expected: parse exception to be throws (indicating bad grammar).
But encountered empty JsonObject (parser stop parsing after it encounters
curly bracket and immediately returns).

I want to provide you with a fix to JsonParser.jj grammar file.

public JsonElement parse():{
  JsonElement json = null;
}
{
  (<EOF>
  | json = JsonObject()
  | json = JsonArray()
  | json = JsonPrimitive()
  | json = JsonNull()) <EOF> {
    return json;
  }
}

Original issue reported on code.google.com by tazija@gmail.com on 4 Apr 2009 at 12:19

GoogleCodeExporter commented 9 years ago
Thanks for the detailed bug report and the suggestions to improve the grammar.

2) can not be done since Gson 1.4 allows multiple JSON objects on the stream. 
So, in your example, it should 
return 3 objects: empty JsonObject(), JsonPrimitive(12), JsonPrimitive(23)

Original comment by inder123 on 23 Sep 2009 at 6:59

GoogleCodeExporter commented 9 years ago
Just realized that the 2 will fail to parse since it is not a valid JSON.
Gson stops at matching {} and doesn't look any further. 

If you were using JsonParserAsync (in 1.4) you will be able to parse "{} 12 23" 
into what I said above.

Original comment by inder123 on 23 Sep 2009 at 7:32

GoogleCodeExporter commented 9 years ago
This is working as we intended it to work.

Note: your use case #1 will return a JsonNull object now.  Not sure if this is 
new
after the 1.4 release.

Original comment by joel.leitch@gmail.com on 23 Sep 2009 at 7:34