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.