Gson序列化字段只有不为空或不为空 [英] Gson Serialize field only if not null or not empty

查看:593
本文介绍了Gson序列化字段只有不为空或不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用的是Gson,但我需要转换器只序列化非null或不空值。



例如:

  //我的java对象看起来像
class TestObject {
String test1;
String test2;
OtherObject otherObject = new OtherObject();
}

现在我的Gson实例将这个对象转换为json看起来像

  Gson gson = new Gson(); 
TestObject obj = new TestObject();
obj.test1 =test1;
obj.test2 =;

字符串jsonStr = gson.toJson(obj);
println jsonStr;

在上面的打印中,结果是

  {test1:test1,test2:,otherObject:{}} 

在这里,我只是想要结果是

  {test1: test1} 

由于test2是空的而otherObject是空的,我不希望他们被序列化为json数据。

顺便说一句,我使用Groovy / Grails,所以如果有任何插件可以做到这一点,如果没有任何建议来定制gson序列化类会很好。

解决方案

创建您自己的 TypeAdapter p>

  public class MyTypeAdapter extends TypeAdapter< TestObject>(){

@Override
public void write (JsonWriter out,TestObject值)抛出IOException {
out.beginObject();
if(!Strings.isNullOrEmpty(value.test1)){
out.name(test1);
out.value(value.test1); $!

$ b if(!Strings.isNullOrEmpty(value.test2)){
out.name(test2);
out.value(value.test1);
}
/ *对otherObject的类似检查* /
out.endObject();
}

@Override
public TestObject read(JsonReader in)throws IOException {
//做类似的事情,但以
}

$ / code>

您可以使用 Gson $ b $ pre $ g $ g $ gson gson = new GsonBuilder()。registerTypeAdapter(TestObject.class,new MyTypeAdapter())。create( );
TestObject obj = new TestObject();
obj.test1 =test1;
obj.test2 =;
System.out.println(gson.toJson(obj));

产生

  {test1:test1} 



GsonBuilder 类有很多方法来创建你自己的序列化/反序列化策略,注册类型适配器,以及设置其他参数。

字符串是一个番石榴类。你是否拥有支票,如果你不想要这种依赖性。


I have requirement where I need to convert java object to json.

I am using Gson for that but i need the converter to only serialize the non null or not empty values.

For example:

//my java object looks like
class TestObject{
    String test1;
    String test2;
    OtherObject otherObject = new OtherObject();
}

now my Gson instance to convert this object to json looks like

Gson gson = new Gson();
TestObject obj = new TestObject();
obj.test1 = "test1";
obj.test2 = "";

String jsonStr = gson.toJson(obj);
println jsonStr;

In the above print, the result is

{"test1":"test1", "test2":"", "otherObject":{}}

Here i just wanted the result to be

{"test1":"test1"}

Since the test2 is empty and otherObject is empty, i don't want them to be serialized to json data.

Btw, I am using Groovy/Grails so if there is any plugin for this that would be good, if not any suggestion to customize the gson serialization class would be good.

解决方案

Create your own TypeAdapter

public class MyTypeAdapter extends TypeAdapter<TestObject>() {

    @Override
    public void write(JsonWriter out, TestObject value) throws IOException {
        out.beginObject();
        if (!Strings.isNullOrEmpty(value.test1)) {
            out.name("test1");
            out.value(value.test1);
        }

        if (!Strings.isNullOrEmpty(value.test2)) {
            out.name("test2");
            out.value(value.test1);
        }
        /* similar check for otherObject */         
        out.endObject();    
    }

    @Override
    public TestObject read(JsonReader in) throws IOException {
        // do something similar, but the other way around
    }
}

You can then register it with Gson.

Gson gson = new GsonBuilder().registerTypeAdapter(TestObject.class, new MyTypeAdapter()).create();
TestObject obj = new TestObject();
obj.test1 = "test1";
obj.test2 = "";
System.out.println(gson.toJson(obj));

produces

 {"test1":"test1"}

The GsonBuilder class has a bunch of methods to create your own serialization/deserialization strategies, register type adapters, and set other parameters.

Strings is a Guava class. Do you own check if you don't want that dependency.

这篇关于Gson序列化字段只有不为空或不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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