Gson自定义序列化与反序列化

jojo
发布于 2020-9-22 11:25
浏览
0收藏

前言


在使用Gson的时候我们经常需要根据自己的需求定义序列化和反序列化的规则,本文主要讲解如何优雅地进行Gson的序列化和反序列化。

 

序列化


方法

 

实现JsonSerializer接口
注册自定义的序列化实现

 

demo

 

自定义UserInformationBeanSerializer并实现JsonSerializer接口

public class UserInformationBeanSerializer implements JsonSerializer<UserInformationBean> {
    @Override
    public JsonElement serialize(UserInformationBean userInformationBean, Type type, JsonSerializationContext jsonSerializationContext) {
        JsonObject Server = new JsonObject();
        JsonObject data = new JsonObject();


        data.addProperty("report", getReport(userInformationBean));
        data.addProperty("time", getTimeStr());

        JsonObject pollData = new JsonObject();
        data.add("pollData", pollData);


        JsonObject basicData = new JsonObject();
        basicData.addProperty("gender", userInformationBean.getSex().ordinal()   "");
        basicData.addProperty("payWay", "0");


        data.add("basicData", basicData);
        Server.add("data", data);

        return Server;

    }



}

 

注册自定义的UserInformationBeanSerializer

UserInformationBeanSerializer userInformationBeanSerializer = new                     UserInformationBeanSerializer();

Type IntListType = new TypeToken<UserInformationBean>() {}.getType();

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(IntListType, userInformationBeanSerializer);

Gson gson = builder.create();

String datacontent = gson.toJson(userInformationBean);

 

反序列化


方法

 

实现 JsonDeserializer 接口
注册自定义的反序列化实现

 

demo

 

自定义TcmJsonBeanDeserializer并实现JsonDeserializer接口

public class TcmJsonBeanDeserializer implements JsonDeserializer<TcmJsonBean> {
  @Override
  public TcmJsonBean deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {


      JsonObject jsonObject = jsonElement.getAsJsonObject();
      TcmJsonBean tcmJsonBean = new TcmJsonBean();

      if (jsonObject.has("代茶饮")) {
          tcmJsonBean.setDaiChaYin(jsonObject.get("代茶饮").getAsString());
      }

      if (jsonObject.has("体质类型")) {
          tcmJsonBean.setDaiChaYin(jsonObject.get("体质类型").getAsString());
      }

      if (jsonObject.has("穴位按摩")) {
          tcmJsonBean.setDaiChaYin(jsonObject.get("穴位按摩").getAsString());
      }

      if (jsonObject.has("运动")) {
          tcmJsonBean.setDaiChaYin(jsonObject.get("运动").getAsString());
      }

      if (jsonObject.has("食疗")) {
          tcmJsonBean.setDaiChaYin(jsonObject.get("食疗").getAsString());
      }


      return null;
  }
}

 

注册自定义的反序列化实现

 String json = readFileToStringUtil.ReadJsonToString(filename);
        Gson gson = new Gson();
        List<JsonElement> list = new ArrayList();
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement = jsonParser.parse(json);  //将json字符串转换成JsonElement
        JsonArray jsonArray = jsonElement.getAsJsonArray();  //将JsonElement转换成JsonArray
        Iterator it = jsonArray.iterator();  //Iterator处理
        tcmJsonBeans = new ArrayList<TcmJsonBean>();

        while (it.hasNext()) {  //循环
            jsonElement = (JsonElement) it.next(); //提取JsonElement
            json = jsonElement.toString();  //JsonElement转换成String
            TcmJsonBean questionInJsonBean = gson.fromJson(json, TcmJsonBean.class); //String转化成JavaBean
            tcmJsonBeans.add(questionInJsonBean);
        }
        return tcmJsonBeans;

 

 

作者:ai-exception

来源:CSDN

分类
收藏
回复
举报
回复
    相关推荐