Gson解析Long为Null和空异常处理

起因
对于一些不规范的json,使用gson解析经常会抛出各种异常导致应用功能失效甚至崩溃
解决
我们希望在接口返回的json异常时,也能解析成功,空值对应的转换为默认值,如:keyId=0;
首先我们希望在数据生成时就能避免出现异常字段,后台帮我们矫正相应字段,其次我们客户端也要针对这种情况进行处理,确保遇到这种情况时,应用能正常运行

通过实现Gson的JsonSerializer接口和JsonDeserializer,即序列化和反序列化接口可以达到该目的

  1. Long类型
public class LongDefault0Adapter implements JsonSerializer, JsonDeserializer {
    @Override
    public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        try {
            if (json.getAsString().equals("") || json.getAsString().equals("null")) {           //定义为long类型,如果后台返回""或者null,则返回0
                return 0;
            }
        } catch (Exception ignore) {
        }
        try {
            return json.getAsLong();
        } catch (NumberFormatException e) {
            throw new JsonSyntaxException(e);
        }
    }
 
    @Override
    public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src);
    }
}
  1. Integer类型
public class IntegerDefault0Adapter implements JsonSerializer, JsonDeserializer {
    @Override
    public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        try {
            if (json.getAsString().equals("") || json.getAsString().equals("null")) {                //定义为int类型,如果后台返回""或者null,则返回0
                return 0;
            }
        } catch (Exception ignore) {
        }
        try {
            return json.getAsInt();
        } catch (NumberFormatException e) {
            throw new JsonSyntaxException(e);
        }
    }
 
    @Override
    public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src);
    }
}
  1. Double类型
public class DoubleDefault0Adapter implements JsonSerializer, JsonDeserializer {
    @Override
    public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)             throws JsonParseException {
        try {
            if (json.getAsString().equals("") || json.getAsString().equals("null")) {          //定义为double类型,如果后台返回""或者null,则返回0.00
                return 0.00;
           }
        } catch (Exception ignore) {
        }
        try {
            return json.getAsDouble();
        } catch (NumberFormatException e) {
            throw new JsonSyntaxException(e);
        }
    }
 
    @Override
    public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src);
    }
}
  1. 使用
/**
 * 增加后台返回""和"null"的处理
 * 1.int=>0
 * 2.double=>0.00
 * 3.long=>0L
 *
 * @return
 */
public static Gson buildGson() {
    if (gson == null) {
        gson = new GsonBuilder()
                .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                .registerTypeAdapter(long.class, new LongDefault0Adapter())
                .create();
    }
    return gson;
}

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