Gson转换错误之输出整形数据变成浮点型数据

在项目开发中,数据库本是整形数据,在经过Gson转换后,输出变成浮点型数据,如输出本为age:1,变为age:1.0,所以要重写Gson的类型转换器。


import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DataTypeAdaptor extends TypeAdapter {
  public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
    @SuppressWarnings("unchecked")
    @Override
    public  TypeAdapter create(Gson gson, TypeToken type) {
      if (type.getRawType() == ServiceListResult.class) {
        return (TypeAdapter) new DataTypeAdaptor(gson);
      }
      return null;
    }
  };

  private final Gson gson;

  DataTypeAdaptor(Gson gson) {
    this.gson = gson;
  }

  @Override
  public void write(JsonWriter out, ServiceListResult value) throws IOException {
    if (value == null) {
      out.nullValue();
      return;
    }

    out.beginObject();//开始接受
    out.name("ReturnCode");//你需要用Gson转换的实体类里的属性
    gson.getAdapter(Integer.class).write(out, value.ReturnCode);
    out.name("Message");//你需要用Gson转换的实体类里的属性
    gson.getAdapter(String.class).write(out, value.Message);
    out.name("Data");//你需要用Gson转换的实体类里的属性
    gson.getAdapter(Object.class).write(out,value.Data);
    out.endObject();
  }

  @Override
  public ServiceListResult read(JsonReader in) throws IOException {
    ServiceListResult data = new ServiceListResult();
    Map dataMap = (Map) readInternal(in);
    data.ReturnCode=((Integer)dataMap.get("ReturnCode"));
    data.Message= (String) dataMap.get("Message");
    data.Data=dataMap.get("Data");
    return data;
  }


  private Object readInternal(JsonReader in) throws IOException {
    JsonToken token = in.peek();
    switch (token) {
      case BEGIN_ARRAY:
        List list = new ArrayList();
        in.beginArray();
        while (in.hasNext()) {
          list.add(readInternal(in));
        }
        in.endArray();
        return list;

      case BEGIN_OBJECT:
        Map map = new LinkedTreeMap();
        in.beginObject();
        while (in.hasNext()) {
          map.put(in.nextName(), readInternal(in));
        }
        in.endObject();
        return map;

      case STRING:
        return in.nextString();

      case NUMBER:
        //将其作为一个字符串读取出来
        //int类型会变double类型主要是读取时是String,然后作为Number处理成Double
        //所以在这里加上判断
        String numberStr = in.nextString();
        //返回的numberStr不会为null
        if (numberStr.contains(".") || numberStr.contains("e")
          || numberStr.contains("E")) {
          return Double.parseDouble(numberStr);
        }
        //return Long.parseLong(numberStr);
        return Integer.parseInt(numberStr);

      case BOOLEAN:
        return in.nextBoolean();

      case NULL:
        in.nextNull();
        return null;

      default:
        throw new IllegalStateException();
    }
  }

}
 
  

接下来就是在需要切换类型转换器(TypeAdapter)的地方注册上述重写的类型转换类

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapterFactory(DataTypeAdaptor.FACTORY);
Gson gson=gsonBuilder.create();

 

你可能感兴趣的:(gson,java,gson)