Gson 布尔值bool与整形int相互转化

Gson 布尔值bool与整形int相互转化


import android.text.TextUtils;

import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * Description
 * author  youxuan  E-mail:[email protected]
 * date createTime:2017/4/8
 * version 1.0.0
 */
public class BooleanTypeAdapter extends TypeAdapter {
    @Override
    public void write(JsonWriter out, Boolean value) throws IOException {
        if (value == null) {
            out.nullValue();
        } else {
            out.value(value);
        }
    }

    @Override
    public Boolean read(JsonReader in) throws IOException {
        JsonToken peek = in.peek();
        switch (peek) {
            case BOOLEAN:
                return in.nextBoolean();
            case NULL:
                in.nextNull();
                return null;
            case NUMBER:
                return in.nextInt() != 0;
            case STRING:
                return toBoolean(in.nextString());
            default:
                throw new JsonParseException("Expected BOOLEAN or NUMBER but was " + peek);
        }
    }

    /**
     * true  TURE 都为true
     * "0" 为 false
     *
     * @param name
     * @return
     */
    public static boolean toBoolean(String name) {
        return (!TextUtils.isEmpty(name))
                &&
                (name.equalsIgnoreCase("true") || !name.equals("0"));
    }
}

模型:

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.icourt.alpha.utils.BooleanTypeAdapter;

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-06-01 16:59
 * 

* 不要轻易修改 * 不要轻易修改 */ public class ResEntity { private static final String FIELD_SUCCEED = "succeed"; private static final String FIELD_MESSAGE = "message"; private static final String FIELD_RESULT = "result"; @SerializedName(value = FIELD_MESSAGE, alternate = {"resultMess"}) public String message; @JsonAdapter(BooleanTypeAdapter.class) @SerializedName(value = FIELD_SUCCEED, alternate = {"resultCode"}) public boolean succeed; @SerializedName(value = FIELD_RESULT, alternate = {"data"}) public T result; @Override public String toString() { return "ResEntity{" + "message='" + message + '\'' + ", succeed=" + succeed + ", result=" + result + '}'; } }


用法 将整形转化为bool 在这个字段上面加上@JsonAdapter(BooleanTypeAdapter.class)

验证:

private void test() {
        String json1 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"message\": \"错误1\",\n" +
                "  \"result\": {},\n" +
                "  \"succeed\": true\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json1, NetRes.class);
            LogUtils.d("---------->netRes1:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常1:" + e);
        }


        String json2 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"message\": \"错误2\",\n" +
                "  \"result\": {},\n" +
                "  \"succeed\": \"1\"\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json2, NetRes.class);
            LogUtils.d("---------->netRes2:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常2:" + e);
        }
        String json3 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"message\": \"错误3\",\n" +
                "  \"result\": {},\n" +
                "  \"succeed\": \"0\"\n" +
                "}";

        try {
            NetRes netRes = JsonUtils.Gson2Bean(json3, NetRes.class);
            LogUtils.d("---------->netRes3:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常3:" + e);
        }

        String json4 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误4\",\n" +
                "  \"result\": {},\n" +
                "  \"succeed\": \"1\"\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json4, NetRes.class);
            LogUtils.d("---------->netRes4:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常4:" + e);
        }

        String json5 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误5\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": \"1\"\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json5, NetRes.class);
            LogUtils.d("---------->netRes5:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常5:" + e);
        }

        String json6 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误6\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": \"0\"\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json6, NetRes.class);
            LogUtils.d("---------->netRes6:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常6:" + e);
        }

        String json7 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误7\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": true\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json7, NetRes.class);
            LogUtils.d("---------->netRes7:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常7:" + e);
        }

        String json8 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误8\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": false\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json8, NetRes.class);
            LogUtils.d("---------->netRes8:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常8:" + e);
        }

        String json9 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误9\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": 0\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json9, NetRes.class);
            LogUtils.d("---------->netRes9:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常9:" + e);
        }

        String json10 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误10\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": 1\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json10, NetRes.class);
            LogUtils.d("---------->netRes10:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常10:" + e);
        }

        String json11 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误11\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": \"\"\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json11, NetRes.class);
            LogUtils.d("---------->netRes11:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常11:" + e);
        }

        String json12 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误12\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\": null\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json12, NetRes.class);
            LogUtils.d("---------->netRes12:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常12:" + e);
        }

        String json13 = "{\n" +
                "  \"detail\": \"返回错误\",\n" +
                "  \"resultMess\": \"错误12\",\n" +
                "  \"result\": {},\n" +
                "  \"resultCode\":\n" +
                "}";
        try {
            NetRes netRes = JsonUtils.Gson2Bean(json13, NetRes.class);
            LogUtils.d("---------->netRes13:" + netRes);
        } catch (JsonParseException e) {
            LogUtils.d("-------->解析异常13:" + e);
        }
    }

Gson 布尔值bool与整形int相互转化_第1张图片

你可能感兴趣的:(android,开发)