将Jackson中的null列表序列化为空数组

huangapple go评论78阅读模式
英文:

Jackson null list to empty array serialization

问题

我想将空的List序列化为空数组。

所以,给定:

class MyBean { List values; }

并且给定一个values为null的实例,它应该被序列化为:

{ "values": [] }

我希望对于所有类中的所有List,都能具有这个全局行为。我不想为类添加任何注解或特定处理。

我已经阅读了所有相关的问题,但没有找到任何有效的方法。似乎无论我尝试为List类注册哪个自定义序列化程序,它都不起作用。

如果您在您的项目中实现了这个,欢迎让我知道您是如何做到的。

英文:

I want to serialize null List to empty array.

So given:

class MyBean { List values; }

And given an instance with null for values, it should be serialized to:

{ "values": [] }

I want this to be global behaviour for all Lists in all classes. I do not want to add any annotations or specific handling for classes.

I have read all related questions I found, and could not come up with anything that works. Seems any custom serializer I try to register for List class is not kicking in.

If you have this working on your project, let me know how did you manage to do that.

答案1

得分: 4

在这种情况下,您需要自定义JacksonAnnotationIntrospector类。要序列化null,默认情况下,Jackson使用com.fasterxml.jackson.databind.ser.std.NullSerializer类。您可以扩展默认的内省类并重写findNullSerializer方法。

以下是示例代码:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setAnnotationIntrospector(new EmptyArrayJacksonAnnotationIntrospector());
        mapper.writeValue(System.out, new ListWrapper());
    }
}

class EmptyArrayJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {

    @Override
    public Object findNullSerializer(Annotated a) {
        if (List.class.isAssignableFrom(a.getRawType())) {
            return ArrayNullSerializer.INSTANCE;
        }
        return super.findNullSerializer(a);
    }
}

final class ArrayNullSerializer extends StdSerializer<Object> {

    public static final ArrayNullSerializer INSTANCE = new ArrayNullSerializer();

    protected ArrayNullSerializer() {
        super(Object.class);
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartArray();
        gen.writeEndArray();
    }
}

class ListWrapper {

    private List values;

    public List getValues() {
        return values;
    }

    public void setValues(List values) {
        this.values = values;
    }
}

以上代码将输出:

{"values":[]}
英文:

In cases like this you need to customize JacksonAnnotationIntrospector class. Te serialize null-s, by default, Jackson uses com.fasterxml.jackson.databind.ser.std.NullSerializer class. You can extend default introspector class and override findNullSerializer.

See below example:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new EmptyArrayJacksonAnnotationIntrospector());
mapper.writeValue(System.out, new ListWrapper());
}
}
class EmptyArrayJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
public Object findNullSerializer(Annotated a) {
if (List.class.isAssignableFrom(a.getRawType())) {
return ArrayNullSerializer.INSTANCE;
}
return super.findNullSerializer(a);
}
}
final class ArrayNullSerializer extends StdSerializer&lt;Object&gt; {
public static final ArrayNullSerializer INSTANCE = new ArrayNullSerializer();
protected ArrayNullSerializer() {
super(Object.class);
}
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartArray();
gen.writeEndArray();
}
}
class ListWrapper {
private List values;
public List getValues() {
return values;
}
public void setValues(List values) {
this.values = values;
}
}

Above code prints:

{&quot;values&quot;:[]}

huangapple
  • 本文由 发表于 2020年8月19日 16:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63482579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定