se new GsonBuilder().create(); MyType target = new MyType(); String json = gson.toJson(target); // serializes target to Json MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
If the object that your are serializing/deserializing is a {@code ParameterizedType}(i.e. contains at least one type parameter and may be an array) then you must use the {@link #toJson(Object,Type)} or {@link #fromJson(String,Type)} method. Here is anexample for serializing and deserialing a {@code ParameterizedType}:
Type listType = new TypeToken<List<String>>() {}.getType(); List<String> target = new LinkedList<String>(); target.add("blah"); Gson gson = new Gson(); String json = gson.toJson(target, listType); List<String> target2 = gson.fromJson(json, listType);
See the Gson User Guide for a more complete set of examples.
@see com.google.gson.reflect.TypeToken
@author Inderjeet Singh
@author Joel Leitch