1、简述
泛型序列化 解决接口类型不能反序列化的问题,采用泛型的形式 来解决接口形式 泛型序列化在嵌套对象的基础上,进一步进行抽象的序列化, 可以实现多态形式的序列化与反序列化 采用 new TypeToken() {}.getType()) 的形式进行反序列化
2、基本类
/** * 泛型形式的单个属性 序列化 * @author sunRainAmazing */ @Data @AllArgsConstructor @NoArgsConstructor public class GsonResData<T> { private Integer id; private String msg; private T data; }
/** * 泛型 集合属性 序列化 * @author sunRainAmazing */ @Data @AllArgsConstructor @NoArgsConstructor public class GsonResListData<T> { private Integer id; private String msg; private List<T> datas; }
/** * 基本类 * @author sunRainAmazing */ @Data @AllArgsConstructor @NoArgsConstructor public class GsonOk { private Integer oid; private String body; private String kind; }
/** * 基本类 * @author sunRainAmazing */ @Data @AllArgsConstructor @NoArgsConstructor public class GsonWarn { private Integer sid; private String header; private String type; }
3、测试类
package sun.rain.amazing.gson.quickstart; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.junit.Test; import sun.rain.amazing.gson.quickstart.extend.GsonFather; import sun.rain.amazing.gson.quickstart.extend.GsonSon; import sun.rain.amazing.gson.quickstart.generic.GsonOk; import sun.rain.amazing.gson.quickstart.generic.GsonResData; import sun.rain.amazing.gson.quickstart.generic.GsonWarn; import java.util.ArrayList; import java.util.List; /** * @author sunRainAmazing */ public class GsonGenericTest { private Gson gson = new Gson(); /** *{"id":101,"msg":"ok","data":{"sid":101,"header":"base-ssl","type":"A"}} * GsonResData(id=101, msg=ok, data={sid=101.0, header=base-ssl, type=A}) * GsonResData(id=101, msg=ok, data=GsonWarn(sid=101, header=base-ssl, type=A)) * * // 上面的采用GsonResData.class 进行反序列化 * 得到的是一个字符串形式的数据, 而并非一个类 (X) * 而通过 new TypeToken>() {}.getType() * 进行反序列化 得到的是 一个类的形式 (√) * ========================================== * * {"id":101,"msg":"ok","data":{"oid":101,"body":"base-ssl","kind":"A"}} * GsonResData(id=101, msg=ok, data={sid=101.0, header=base-ssl, type=A}) * GsonResData(id=101, msg=ok, data=GsonOk(oid=null, body=null, kind=null)) * GsonResData(id=101, msg=ok, data={oid=101.0, body=base-ssl, kind=A}) * GsonResData(id=101, msg=ok, data=GsonOk(oid=101, body=base-ssl, kind=A)) * * // 解析类 与 接收类不同 则 得不到对应的属性 * GsonResData(id=101, msg=ok, data=GsonWarn(sid=null, header=null, type=null)) */ @Test public void testGsonGeneric(){ GsonWarn gf = new GsonWarn(101,"base-ssl","A"); GsonResData gr = new GsonResData<>(101,"ok",gf); // json 序列化 String json = gson.toJson(gr); System.out.println(json); // json 反序列化 GsonResData u = gson.fromJson(json,GsonResData.class); System.out.println(u); GsonResData u1 = gson.fromJson(json, new TypeToken >() {}.getType()); System.out.println(u1); System.out.println("=========================================="); GsonOk go = new GsonOk(101,"base-ssl","A"); GsonResData grd = new GsonResData<>(101,"ok",go); // json 序列化 String jsonRight = gson.toJson(grd); System.out.println(jsonRight); // json 反序列化 泛型的形式 GsonResData ur = gson.fromJson(json,GsonResData.class); System.out.println(ur); GsonResData ur1 = gson.fromJson(json, new TypeToken >() {}.getType()); System.out.println(ur1); // 采用正确的json 进行序列化 -- 则 反序列化的结果是 满足需求的 GsonResData urRight = gson.fromJson(jsonRight,GsonResData.class); System.out.println(urRight); GsonResData urRight1 = gson.fromJson(jsonRight, new TypeToken >() {}.getType()); System.out.println(urRight1); // 采用的解析类 GsonResData 与 接收类 GsonResData 不同 GsonResData urRight2 = gson.fromJson(jsonRight, new TypeToken >() {}.getType()); System.out.println(urRight2); } /** * {"id":101,"msg":"ok", * "data":[{"sid":101,"header":"base-ssl","type":"A"}, * {"sid":102,"header":"base-ddl","type":"B"}]} * * GsonResData(id=101, msg=ok, * data=[{sid=101.0, header=base-ssl, type=A}, * {sid=102.0, header=base-ddl, type=B}]) * * GsonResData(id=101, msg=ok, * data=[GsonWarn(sid=101, header=base-ssl, type=A), * GsonWarn(sid=102, header=base-ddl, type=B)]) */ @Test public void testGsonGenericList(){ GsonWarn gf = new GsonWarn(101,"base-ssl","A"); GsonWarn gf1 = new GsonWarn(102,"base-ddl","B"); List list = new ArrayList<>(); list.add(gf); list.add(gf1); GsonResData > gr = new GsonResData<>(101,"ok",list); // json 序列化 String json = gson.toJson(gr); System.out.println(json); // json 反序列化 测试 list的形式 GsonResData
u = gson.fromJson(json,GsonResData.class); System.out.println(u); GsonResData u1 = gson.fromJson(json, new TypeToken >>() {}.getType()); System.out.println(u1); } }
测试2
package sun.rain.amazing.gson.quickstart; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.junit.Test; import sun.rain.amazing.gson.quickstart.generic.GsonOk; import sun.rain.amazing.gson.quickstart.generic.GsonResData; import sun.rain.amazing.gson.quickstart.generic.GsonResListData; import sun.rain.amazing.gson.quickstart.generic.GsonWarn; import java.util.ArrayList; import java.util.List; /** * @author sunRainAmazing */ public class GsonGenericListTest { private Gson gson = new Gson(); /** *{"id":101,"msg":"ok", * "datas":[{"sid":101,"header":"base-ssl","type":"A"}, * {"sid":102,"header":"bas2-ddl","type":"B"}]} * * GsonResListData(id=101, msg=ok, * datas=[{sid=101.0, header=base-ssl, type=A}, * {sid=102.0, header=bas2-ddl, type=B}]) * * GsonResListData(id=101, msg=ok, * datas=[GsonWarn(sid=101, header=base-ssl, type=A), * GsonWarn(sid=102, header=bas2-ddl, type=B)]) */ @Test public void testGsonGenericList(){ GsonWarn gf = new GsonWarn(101,"base-ssl","A"); GsonWarn gf1 = new GsonWarn(102,"bas2-ddl","B"); Listlist = new ArrayList<>(); list.add(gf);list.add(gf1); GsonResListData gr = new GsonResListData<>(101,"ok",list); // json 序列化 String json = gson.toJson(gr); System.out.println(json); // json 反序列化 进行构造 new TokenType(){}.getType() GsonResListData u = gson.fromJson(json,GsonResListData.class); System.out.println(u); GsonResListData u1 = gson.fromJson(json, new TypeToken >() {}.getType()); System.out.println(u1); } }