美文网首页
Gson复杂对象自定义序列化类

Gson复杂对象自定义序列化类

作者: 黄金矿工00七 | 来源:发表于2017-10-09 19:57 被阅读0次
  • 实体类
package com.xqxf.dto;

import com.xqxf.domain.User;

/**
 * Package com.xqxf.dto
 *
 * author liyaocai
 *
 * date 2017/10/7 20:24
 *
 * Description:
 *
 * version V1.0
 */

public class UserDto {
private String uId;
private User user;

  public UserDto() {
  }

  public String getuId() {
    return uId;
  }

  public void setuId(String uId) {
    this.uId = uId;
  }

  public User getUser() {
    return user;
  }

  public void setUser(User user) {
    this.user = user;
  }
}

自定义序列化类继承JsonDeserializer

  • UserDeserializer类
package com.xqxf.utils.Json;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.xqxf.domain.User;
import com.xqxf.dto.UserDto;
import java.lang.reflect.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Package com.xqxf.utils
 *
 * author liyaocai
 *
 * date 2017/10/7 19:49
 *
 * Description:
 *
 * version V1.0
 */

public class UserDeserializer  implements JsonDeserializer<UserDto>{
Logger logger= LoggerFactory.getLogger(UserDeserializer.class);
  public UserDeserializer() {
  }

  @Override
  public UserDto deserialize(JsonElement jsonElement, Type type,
      JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject=jsonElement.getAsJsonObject();
    User user=new User();

    UserDto userDto=new UserDto();
    user.setNickName(jsonObject.get("info").getAsJsonObject().get("nickName").getAsString());
//    user.setPermission(jsonObject.get("info").getAsJsonObject().get("permission").getAsString());
//  user.setUserName(jsonObject.get("info").getAsJsonObject().get("userName").getAsString());
    user.setAddress(jsonObject.get("info").getAsJsonObject().get("address").getAsString());
    user.setCompany(jsonObject.get("info").getAsJsonObject().get("company").getAsString());
    user.setDepartment(jsonObject.get("info").getAsJsonObject().get("department").getAsString());
    user.setPhone(jsonObject.get("info").getAsJsonObject().get("phone").getAsString());
    user.setTel(jsonObject.get("info").getAsJsonObject().get("tel").getAsString());
    userDto.setuId(jsonObject.get("uId").getAsString());

    userDto.setUser(user);
    return userDto;
  }


}

  • 使用建造者模式构件gson实例
 Gson gson = new GsonBuilder()//建造者模式设置不同的配置
      .serializeNulls()//序列化为null对象
      .setDateFormat("yyyy-MM-dd HH:mm:ss") //设置日期的格式
      .disableHtmlEscaping()//防止对网址乱码 忽略对特殊字符的转换
      .excludeFieldsWithoutExposeAnnotation()
//      使用自定义适配器
      .registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory())
      .registerTypeAdapter(UserDto.class,new UserDeserializer())
      .registerTypeAdapter(WorkOrderDto.class,new WorkOrderDeserializer())
      .create();

关于JsonDeserializer的介绍请查看http://www.importnew.com/16786.html

相关文章

网友评论

      本文标题:Gson复杂对象自定义序列化类

      本文链接:https://www.haomeiwen.com/subject/fnnpyxtx.html