Closed GoogleCodeExporter closed 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
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
Original issue reported on code.google.com by
dan.cres...@gmail.com
on 30 Dec 2008 at 2:30Attachments: