簡體   English   中英

如何防止 Map 中的空值和 bean 中的空字段通過 Jackson 序列化

[英]How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson

我有一個Map<String,Foo> foosMap我想通過 Jackson 序列化。 現在我想在序列化過程中遵循兩個設置:

  1. Map 可以有很多空值和空鍵,我不希望空值被序列化。
  2. 對於所有被序列化的 Foos,我不想序列化 Foo 中引用的空對象。

實現這一目標的最佳方法是什么? 我在我的項目中使用 jackson-core1.9 和 jackson-mapper1.9 罐子。

如果更改要序列化的原始Map數據結構以更好地表示要序列化的實際值是合理的,那可能是一種不錯的方法,這可能會減少所需的 Jackson 配置量。 例如,如果可能,在調用 Jackson 之前只刪除null鍵條目。 那說...


要禁止序列化具有空值的Map條目:

傑克遜 2.9 之前

您仍然可以使用WRITE_NULL_MAP_VALUES ,但請注意它已移至SerializationFeature

mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

自傑克遜 2.9

WRITE_NULL_MAP_VALUES已棄用,您可以使用以下等效項:

mapper.setDefaultPropertyInclusion(
   JsonInclude.Value.construct(Include.ALWAYS, Include.NON_NULL))

要禁止使用空值序列化屬性,您可以直接配置ObjectMapper ,或使用@JsonInclude注釋:

mapper.setSerializationInclusion(Include.NON_NULL);

要么:

@JsonInclude(Include.NON_NULL)
class Foo
{
  public String bar;

  Foo(String bar)
  {
    this.bar = bar;
  }
}

據我所知,要處理空Map鍵,一些自定義序列化是必要的。

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

要禁止使用null鍵序列化Map條目,需要進一步的自定義序列化處理。

對於 Jackson 版本 < 2.0,在被序列化的類上使用此注釋:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

答案似乎有點老了,我所做的是使用這個映射器來轉換一個MAP

 ObjectMapper mapper = new ObjectMapper().configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);

一個簡單的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);

例如,像這樣使用它:

 String json = mapper.writeValueAsString(user);

我的解決方案,希望有所幫助
自定義 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM