package com.ydl.ydlnet.builder.gson; import com.google.gson.*; import java.lang.reflect.Type; /** * Created by haorui on 2019-09-02 . * Des: 定义为int类型,如果后台返回""或者null,则返回0 */ public class IntegerDefault0Adapter implements JsonSerializer<Integer>, JsonDeserializer<Integer> { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) { 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); } }