import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class Class1 {
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
l.add("foo");
l.add("bar");
Type t = new TypeToken<List<String>>(){}.getType();
System.out.println(new Gson().toJson(l, t));
}
}
However, Gson 2.0 doesn't seem to need the Type in order to serialize
the list. This code works just fine:
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class Class1 {
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
l.add("foo");
l.add("bar");
System.out.println(new Gson().toJson(l));
}
}
I based my use of TypeToken on the API docs (http://google-
gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/
Gson.html) and the User Guide (https://sites.google.com/site/gson/gson-
user-guide#TOC-Serializing-and-Deserializing-Gener).
What am I doing wrong?
Thanks,
-Peter
Are you using ProGuard or another tool that could be interfering with
type information?