[英]How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson
I have aa Map<String,Foo> foosMap
that I want to serialize through Jackson.我有一个Map<String,Foo> foosMap
我想通过 Jackson 序列化。 Now I want following two settings on the serialization process:现在我想在序列化过程中遵循两个设置:
What is the best way to achieve this?实现这一目标的最佳方法是什么? I am using jackson-core1.9 and jackson-mapper1.9 jars in my project.我在我的项目中使用 jackson-core1.9 和 jackson-mapper1.9 罐子。
If it's reasonable to alter the original Map
data structure to be serialized to better represent the actual value wanted to be serialized, that's probably a decent approach, which would possibly reduce the amount of Jackson configuration necessary.如果更改要序列化的原始Map
数据结构以更好地表示要序列化的实际值是合理的,那可能是一种不错的方法,这可能会减少所需的 Jackson 配置量。 For example, just remove the null
key entries, if possible, before calling Jackson.例如,如果可能,在调用 Jackson 之前只删除null
键条目。 That said...那说...
To suppress serializing Map
entries with null values:要禁止序列化具有空值的Map
条目:
Before Jackson 2.9杰克逊 2.9 之前
you can still make use of WRITE_NULL_MAP_VALUES
, but note that it's moved to SerializationFeature
:您仍然可以使用WRITE_NULL_MAP_VALUES
,但请注意它已移至SerializationFeature
:
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
Since Jackson 2.9自杰克逊 2.9
The WRITE_NULL_MAP_VALUES
is deprecated, you can use the below equivalent: WRITE_NULL_MAP_VALUES
已弃用,您可以使用以下等效项:
mapper.setDefaultPropertyInclusion(
JsonInclude.Value.construct(Include.ALWAYS, Include.NON_NULL))
To suppress serializing properties with null values, you can configure the ObjectMapper
directly , or make use of the @JsonInclude
annotation:要禁止使用空值序列化属性,您可以直接配置ObjectMapper
,或使用@JsonInclude
注释:
mapper.setSerializationInclusion(Include.NON_NULL);
or:要么:
@JsonInclude(Include.NON_NULL)
class Foo
{
public String bar;
Foo(String bar)
{
this.bar = bar;
}
}
To handle null Map
keys, some custom serialization is necessary, as best I understand.据我所知,要处理空Map
键,一些自定义序列化是必要的。
A simple approach to serialize null
keys as empty strings (including complete examples of the two previously mentioned configurations):将null
键序列化为空字符串的简单方法(包括前面提到的两个配置的完整示例):
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
public class JacksonFoo
{
public static void main(String[] args) throws Exception
{
Map<String, Foo> foos = new HashMap<String, Foo>();
foos.put("foo1", new Foo("foo1"));
foos.put("foo2", new Foo(null));
foos.put("foo3", null);
foos.put(null, new Foo("foo4"));
// System.out.println(new ObjectMapper().writeValueAsString(foos));
// Exception: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.getSerializerProvider().setNullKeySerializer(new MyNullKeySerializer());
System.out.println(mapper.writeValueAsString(foos));
// output:
// {"":{"bar":"foo4"},"foo2":{},"foo1":{"bar":"foo1"}}
}
}
class MyNullKeySerializer extends JsonSerializer<Object>
{
@Override
public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused)
throws IOException, JsonProcessingException
{
jsonGenerator.writeFieldName("");
}
}
class Foo
{
public String bar;
Foo(String bar)
{
this.bar = bar;
}
}
To suppress serializing Map
entries with null
keys, further custom serialization processing would be necessary.要禁止使用null
键序列化Map
条目,需要进一步的自定义序列化处理。
For Jackson versions < 2.0 use this annotation on the class being serialized:对于 Jackson 版本 < 2.0,在被序列化的类上使用此注释:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
Answer seems to be a little old, What I did was to use this mapper to convert a MAP
答案似乎有点老了,我所做的是使用这个映射器来转换一个MAP
ObjectMapper mapper = new ObjectMapper().configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
a simple Map
:一个简单的Map
:
Map<String, Object> user = new HashMap<String,Object>(); user.put( "id", teklif.getAccount().getId() ); user.put( "fname", teklif.getAccount().getFname()); user.put( "lname", teklif.getAccount().getLname()); user.put( "email", teklif.getAccount().getEmail()); user.put( "test", null);
Use it like this for example:例如,像这样使用它:
String json = mapper.writeValueAsString(user);
my solution, hope help我的解决方案,希望有所帮助
custom ObjectMapper and config to spring xml(register message conveters)自定义 ObjectMapper 并配置为 spring xml(注册消息转换器)
public class PyResponseConfigObjectMapper extends ObjectMapper {
public PyResponseConfigObjectMapper() {
disable(SerializationFeature.WRITE_NULL_MAP_VALUES); //map no_null
setSerializationInclusion(JsonInclude.Include.NON_NULL); // bean no_null
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.