I have the following code
import com.google.gson.Gson; public class JavaApplication18 { public static class Holder { public Object value; } public static void main(String[] args) { Gson gson = new Gson(); Integer i = new Integer(123); Holder holder = new Holder(); holder.value = i; String json = gson.toJson(holder); System.out.println(json); Holder newHolder = gson.fromJson(json, Holder.class); System.out.println(newHolder.value.getClass()); } }
Output signal
{"value":123} class java.lang.Double
I want Gson to save type information when it serializes / deserializes by type of object. Is there any elegant way to achieve this? Or is it impossible?
source share