Closed GoogleCodeExporter closed 9 years ago
Original comment by joel.leitch@gmail.com
on 31 Jul 2008 at 12:59
You need to handle top-level "Parameterized" types differently than Class
objects.
Due to "Type Erasure" in the Java language, when you pass in a instance of the
"Map"
object, all generic information is lost. To get around this problem, we
overloaded
the toJson method to take a "Type" parameter.
Here is an example:
import com.google.gson.reflect.TypeToken;
public class TopLevelMapExample {
public static void main(String[] args) {
Type mapType = new TypeToken<Map<String, String>>() {}.getType();
String json = new Gson().toJson(new HashMap<String, String>(), mapType);
System.out.println(json);
}
}
----
I am updating the code to throw an IllegalArgumentException with a very
descriptive
message.
Original comment by joel.leitch@gmail.com
on 31 Jul 2008 at 7:25
r128 changed the code to raise a IllegalArgumentException instead.
Original comment by joel.leitch@gmail.com
on 31 Jul 2008 at 7:57
Original issue reported on code.google.com by
cedric.v...@gmail.com
on 30 Jul 2008 at 9:57Attachments: