简单的gson对boolean转换的test(作为备注)

16 篇文章 0 订阅

GsonBooleanTest.java

package com.test;

import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

/**
 * @author John Kenrinus Lee
 * @version 2016-08-19
 */
public class GsonBooleanTest {
    public static void main(String[] args) {
        Mod mod = null;
        // see isTd
        String xx1 = "{\"isTd\":\"0\", \"mDd\":\"hello\"}"; // false
        String xx2 = "{\"isTd\":\"1\", \"mDd\":\"hello\"}"; // false
        String xx3 = "{\"isTd\":true, \"mDd\":\"hello\"}"; // true
        String xx4 = "{\"isTd\":\"true\", \"mDd\":\"hello\"}"; // true
        String xx5 = "{\"isTd\":false, \"mDd\":\"hello\"}"; // false
        String xx6 = "{\"isTd\":\"false\", \"mDd\":\"hello\"}"; // false
        String xx7 = "{\"isTd\":1, \"mDd\":\"hello\"}"; // throw; if use adapter, then true
        String xx8 = "{\"isTd\":0, \"mDd\":\"hello\"}"; // throw; if use adapter, then false
        String xx9 = "{\"isTd\":\"Yes\", \"mDd\":\"hello\"}"; // false
        String xx10 = "{\"isTd\":\"No\", \"mDd\":\"hello\"}"; // false
        Gson gson = new Gson();
        mod = gson.fromJson(xx1, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx2, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx3, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx4, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx5, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx6, Mod.class);
        System.out.println(mod.isTd);
        gson = new GsonBuilder().registerTypeAdapter(Mod.class, new TypeAdapter<Mod>() {
            @Override
            public void write(JsonWriter out, Mod value) throws IOException {
                out.beginObject();
                out.name("isTd").value(value.isTd);
                out.name("mDd").value(value.mDd);
                out.endObject();
            }
            @Override
            public Mod read(JsonReader in) throws IOException {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                    return null;
                }
                in.beginObject();
                final Mod mod = new Mod();
                while (in.hasNext()) {
                    switch (in.nextName()) {
                        case "isTd":
                            if (in.peek() == JsonToken.BOOLEAN) {
                                mod.isTd = in.nextBoolean();
                            } else if (in.peek() == JsonToken.NUMBER) {
                                mod.isTd = in.nextInt() != 0;
                            } else if (in.peek() == JsonToken.STRING) {
                                String bool = in.nextString();
                                try {
                                    if (bool.equalsIgnoreCase("true") || bool.equalsIgnoreCase("yes")
                                            || Integer.parseInt(bool) != 0) {
                                        mod.isTd = true;
                                    }
                                } catch (Exception ignored) {
                                   // Do nothing
                                }
                            } else {
                                throw new JsonSyntaxException("Unknown type for isTd");
                            }
                            break;
                        case "mDd":
                            mod.mDd = in.nextString();
                            break;
                    }
                }
                in.endObject();
                return mod;
            }
        }).create();
        mod = gson.fromJson(xx7, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx8, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx9, Mod.class);
        System.out.println(mod.isTd);
        mod = gson.fromJson(xx10, Mod.class);
        System.out.println(mod.isTd);
    }
}

Mod.java

public class Mod {
    public boolean isTd;
    public String mDd;
}

php中0, 1, ios中Yes, No, 可能都算是boolean, 但在java的gson中转换失灵, 需要adapter.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值