GSON序列化布尔值为0或1 [英] GSON Serialize boolean to 0 or 1

查看:316
本文介绍了GSON序列化布尔值为0或1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我正在尝试以下操作:

  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);
返回retVal;

所以这个小片段会产生:

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

但我需要它产生的是:

pre $ {x:0, y:10,z:ZZZ}

有人可以给我一些选择?我不想重写我的布尔值作为整数,因为这会导致与现有代码的几个问题(不明显,难以阅读,难以执行等)。

解决方案

为了使其正确的方式,你可以使用类似的东西

  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;

公共类BooleanSerializer实现了JsonSerializer< Boolean> ;, JsonDeserializer< Boolean> {

@Override
public JsonElement serialize(Boolean arg0,Type arg1,JsonSerializationContext arg2){
return new JsonPrimitive(Boolean.TRUE.equals(arg0));

$ b @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();
保护字符串控制=ctrl;
}

public class Child extends Base {
protected String text =This is text;
protected Boolean boolTest = false;

$ b $ **
* @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));


希望这有助于某人: - )


All,

I'm trying to do the following:

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;

So this little snippet will produce:

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

but what I need it to produce is:

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

Can someone please give me some options? I'd prefer not to rewrite my booleans as ints as that will cause several issues with existing code (nonobvious, hard to read, hard to enforce, etc.)

解决方案

To make it "correct" way, you can use something like that

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;
    }
}

And then use it:

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));
    }
}

Hope this helps someone :-)

这篇关于GSON序列化布尔值为0或1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆