This may not be a bug, but the behavior surprised me (so I figured I should
mention it).
The Gson.toJson(Object) method handles JsonElement values differently than the
Gson.toJson(JsonElement) method. This can come up when you have heterogeneous
collections, for example, where only some (but not all) of the elements are
JsonElement values.
Here's a sample program.
import com.google.gson.*;
public class Test
{
public static void
main(String[] args)
{
String input = "{\"name\": \"lars\", \"age\": 12}";
Object obj = new JsonParser().parse(input).getAsJsonObject();
String output = new Gson().toJson(obj);
System.out.println(output);
/*
expected output:
either `{"name":"lars","age":12}`
or `{"age":12,"name":"lars"}`
actual output:
`{"members":{"name":{"value":"lars"},"age":{"value":12}}}`
*/
}
(In case it's not clear, I was surprised to see the `members` and the `value`
fields in the output.)
Original issue reported on code.google.com by von...@gmail.com on 31 Aug 2011 at 5:14
Original issue reported on code.google.com by
von...@gmail.com
on 31 Aug 2011 at 5:14