discomarathon / google-gson

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

Unexpected EOF #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Compile the attached code against gson 1.2.3: javac -classpath
~/src/gossip/thirdpartylib/gson-1.2.3.jar Test.java
2. Run the code: java -classpath
~/src/gossip/thirdpartylib/gson-1.2.3.jar:. Test

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

Expect to see:
My buffer contains: 9 bytes
abcdef
1

But get:
My buffer contains: 9 bytes
abcdef
Exception in thread "main" com.google.gson.JsonParseException: Failed
parsing JSON source: java.io.InputStreamReader@5856a5 to Json
    at com.google.gson.Gson.fromJson(Gson.java:378)
    at com.google.gson.Gson.fromJson(Gson.java:344)
    at Test.main(Test.java:19)
Caused by: com.google.gson.ParseException: Encountered "<EOF>" at line 0,
column 0.
Was expecting one of:
    <DIGITS> ...
    <SINGLE_QUOTE_LITERAL> ...
    <DOUBLE_QUOTE_LITERAL> ...
    "{" ...
    "null" ...
    "[" ...
    "true" ...
    "false" ...
    "-" ...

    at com.google.gson.JsonParser.generateParseException(JsonParser.java:435)
    at com.google.gson.JsonParser.jj_consume_token(JsonParser.java:374)
    at com.google.gson.JsonParser.parse(JsonParser.java:29)
    at com.google.gson.Gson.fromJson(Gson.java:370)
    ... 2 more

What version of the product are you using? On what operating system?

gson-1.2.3 on OSX 10.5.5

Please provide any additional information below.

Not sure if this is correct usage of Gson....

Original issue reported on code.google.com by dan.cres...@gmail.com on 30 Dec 2008 at 2:30

Attachments:

GoogleCodeExporter commented 9 years ago
I looked at the test that you provided and Gson was not built for this kind of 
Test
Case.  Gson is a tool for converting a JSON structure to a Java object and vice 
versa.

This test case outputs two objects to the Writer, a String and an Integer.  
Therefore
the writer buffer looks like:
"abcdef"1

The above is invalid JSON.  If you'd like to output a String and integer as 
JSON then
you should create a model class the contains these as fields.  For example:

public class Foo {
  private final String a;
  private final int b;

  public Foo() {
    this("", 0);
  }

  public Foo(String a, int b) {
    this.a = a;
    this.b = b;
  }
}

Foo foo = new Foo("abcdef", 1);
myGson.toJson(foo, myWriter);
Foo anotherFoo = myGson.fromJson(myReader, Foo.class);

Or

Foo foo = new Foo("abcdef", 1);
String json = myGson.toJson(foo);
Foo anotherFoo = myGson.fromJson(json, Foo.class);

NOTE: Foo will be represented as the following JSON structure:
{"a":"abcdef","b":1}

I would be possible for us to allow multiple objects to be parsed from a Reader
object, but I for now I am lowering the Priority of this bug as I feel this a
somewhat of a special case.  Feel free to argue otherwise if the above 
explanation
does not help your use case. 

Original comment by joel.leitch@gmail.com on 6 Jan 2009 at 5:36

GoogleCodeExporter commented 9 years ago
This is not a bug as it was invalid JSON.  The above suggestion to serialize 
based on
the newline in a writer doesn't makes sense either because JSON get be 
submitted with
newlines.

Closing this off as Gson support serializing a JSON structure to a Java object.

Original comment by joel.leitch@gmail.com on 27 Mar 2009 at 6:38