【问题标题】:GSON Serialize boolean to 0 or 1GSON 将布尔值序列化为 0 或 1
【发布时间】:2012-03-07 18:29:35
【问题描述】:

全部,

我正在尝试执行以下操作:

public class SomClass
{
    public boolean x;
    public int y;
    public String z;
}       

SomClass s = new SomClass();
s.x = true;
s.y = 10;
s.z = "ZZZ";
Gson gson = new Gson();
String retVal = gson.toJson(s);
return retVal;

所以这个小sn-p会产生:

{"x":true,"y":10,"z":"ZZZ"}

但我需要它产生的是:

{"x":0, "y":10,"z":"ZZZ"}

有人可以给我一些选择吗?我不希望将我的布尔值重写为整数,因为这会导致现有代码出现一些问题(不明显、难以阅读、难以执行等)

【问题讨论】:

    标签: java json parsing serialization gson


    【解决方案1】:

    为了让它“正确”的方式,你可以使用类似的东西

    import java.lang.reflect.Type;
    
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParseException;
    import com.google.gson.JsonPrimitive;
    import com.google.gson.JsonSerializationContext;
    import com.google.gson.JsonSerializer;
    
    public class BooleanSerializer implements JsonSerializer<Boolean>, JsonDeserializer<Boolean> {
    
        @Override
        public JsonElement serialize(Boolean arg0, Type arg1, JsonSerializationContext arg2) {
            return new JsonPrimitive(Boolean.TRUE.equals(arg0));
        }
    
        @Override
        public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
            return arg0.getAsInt() == 1;
        }
    }
    

    然后使用它:

    public class Main {
    
        public class Base {
            @Expose
            @SerializedName("class")
            protected String clazz = getClass().getSimpleName();
            protected String control = "ctrl";
        }
    
        public class Child extends Base {
            protected String text = "This is text";
            protected Boolean boolTest = false;
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Main m = new Main();
            GsonBuilder b = new GsonBuilder();
            BooleanSerializer serializer = new BooleanSerializer();
            b.registerTypeAdapter(Boolean.class, serializer);
            b.registerTypeAdapter(boolean.class, serializer);
            Gson gson = b.create();
    
            Child c = m.new Child();
            System.out.println(gson.toJson(c));
            String testStr = "{\"text\":\"This is text\",\"boolTest\":1,\"class\":\"Child\",\"control\":\"ctrl\"}";
            Child cc = gson.fromJson(testStr, Main.Child.class);
            System.out.println(gson.toJson(cc));
        }
    }
    

    希望这可以帮助某人:-)

    【讨论】:

    • 正如 IntelliJ 为我指出的,您可以将 deserialize 函数简化为简单的 return arg0.getAsInt() == 1
    • 稍作修改即可为我工作:b.registerTypeAdapter(boolean.class, new BooleanSerializer());
    • 感谢它为我工作...b.registerTypeAdapter(boolean.class, new BooleanSerializer());和@Gavin 一样
    • 根据文档google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/…,您还应该注册 boolean.class b.registerTypeAdapter(Boolean.class, new BooleanSerializer()); b.registerTypeAdapter(boolean.class, new BooleanSerializer());
    • return new JsonPrimitive(arg0 ? 1 : 0);如果 arg0 为空,将会崩溃。做 Boolean.TRUE.equals(arg0) ... 代替。
    【解决方案2】:
    猜你喜欢
    • 2010-09-10
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    相关资源
    最近更新 更多