Jackson中处理map中的null key 或者null value 及实体字段中的null value
Posted guanbin-529
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jackson中处理map中的null key 或者null value 及实体字段中的null value相关的知识,希望对你有一定的参考价值。
1.map中有null key时的序列化
当有null key时,jackson序列化会报 Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)
处理此异常有两种方式
- 1.需要自定义一个序列化null key的方法
- 2. map中直接remove null key
这里只讨论第一种:
处理方法为 mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer()); 将null key处理为空字符
@Test public void testSerializMapNullKey() throws JsonProcessingException //ignore map null value Map<String, Object> map = new HashMap<>(); map.put(null, "test"); ObjectMapper mapper = new ObjectMapper(); mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer()); System.out.println(mapper.writeValueAsString(map)); static class NullKeySerializer extends StdSerializer<Object> public NullKeySerializer() this(null); public NullKeySerializer(Class<Object> t) super(t); @Override public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused) throws IOException, JsonProcessingException jsonGenerator.writeFieldName("");
"":"test" Process finished with exit code 0
2. 处理null value的情况
@Test public void testIgnoreMapNullValue() throws JsonProcessingException //ignore null key Map<String, Object> map = new HashMap<>(); map.put("key", "test"); map.put("key1", null); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); System.out.println(mapper.writeValueAsString(map));
输出为:"key":"test" 并没有 key1:null
"key":"test" Process finished with exit code 0
3. 处理实体中filed中值为null的情况
使用注解:@JsonInclude(JsonInclude.Include.NON_NULL)
注意:@JsonInclude(JsonInclude.Include.NON_NULL)即可以在实体上用,也可以在filed中使用,比如在name上用这个注解
@Test public void testIgnoreNullFiled() throws JsonProcessingException //test ignore null filed User user = new User(); user.setName(null); user.setHouse("asdf"); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writeValueAsString(user)); /** * ignore null filed */ @JsonInclude(JsonInclude.Include.NON_NULL) class User private String name; private String house; public String getName() return name; public void setName(String name) this.name = name; public String getHouse() return house; public void setHouse(String house) this.house = house;
输出为:
"house":"asdf" Process finished with exit code 0
如果设置全局怎么设置呢,使用: mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@Test public void testIgnoreNullFiledGlobally() throws JsonProcessingException //test ignore null filed User user = new User(); user.setName(null); user.setHouse("asdf"); ObjectMapper mapper = new ObjectMapper(); //Ignore Null Fields Globally mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); System.out.println(mapper.writeValueAsString(user));
参考:1.https://www.baeldung.com/jackson-map-null-values-or-null-key
2.https://github.com/eugenp/tutorials/tree/master/jackson-simple
以上是关于Jackson中处理map中的null key 或者null value 及实体字段中的null value的主要内容,如果未能解决你的问题,请参考以下文章
MessagePack Java Jackson Dataformat 在 Map 中不使用 String 为 Key 的序列化