package com.yeshi.fanli.adapter; import java.lang.reflect.Type; import net.sf.json.JSONObject; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.yeshi.fanli.entity.bus.user.UserInfo; public class UserInfoAdapter implements JsonSerializer, JsonDeserializer { // json转为对象时调�?实现JsonDeserializer接口 @Override public UserInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return null; } // 对象转为Json时调�?实现JsonSerializer接口 @Override public JsonElement serialize(UserInfo src, Type typeOfSrc, JsonSerializationContext context) { JSONObject data = new JSONObject(); data.put("id", src.getId()); data.put("nickName", src.getNickName()); data.put("portrait", src.getPortrait()); JsonElement json = new JsonPrimitive(data.toString()); return json; } }