java Jackson 反序列化器 - 将空集合更改为空集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32398885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:08:53  来源:igfitidea点击:

Hymanson deserializer - change null collection to empty one

javajsonHymanson

提问by pepuch

I have an entity that contains collection as attribute:

我有一个包含集合作为属性的实体:

public class Entity {

    @JsonProperty(value="homes")
    @JsonDeserialize(as=HashSet.class, contentAs=HomeImpl.class)
    private Collection<Home> homes = new ArrayList<Home>();

}

If request contains null as request property:

如果请求包含 null 作为请求属性:

{
  "homes": null
}

then homes is set to null. What I want to do is to set homes to empty list. Do I need to write special deserializer for this or is there one for collections? What I tried is this deserializer but it looks ugly (it's not generic and uses implementation instead of interface).

然后homes设置为null。我想要做的是将家设置为空列表。我需要为此编写特殊的反序列化器还是有一个用于集合?我尝试的是这个反序列化器,但它看起来很难看(它不是通用的,并且使用实现而不是接口)。

public class NotNullCollectionDeserializer extends JsonDeserializer<Collection<HomeImpl>> {

  @Override
  public Collection<HomeImpl> deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    return jsonParser.readValueAs(new TypeReference<Collection<HomeImpl>>(){});
  }

  @Override
  public Collection<HomeImpl> getNullValue() {
    return Collections.emptyList();
  }
}

So few questions:

这么几个问题:

  1. Is there some Hymanson property that changes null to empty collection during deserialization?
  2. If no for the first point - do I need to write deserializer for this? If yes, can I write generic one?
  1. 是否有一些 Hymanson 属性在反序列化期间将 null 更改为空集合?
  2. 如果第一点没有 - 我是否需要为此编写反序列化器?如果是,我可以写一个通用的吗?

采纳答案by Manos Nikolaidis

I also couldn't find a Hymanson property or annotation for this. So I'll have to answer no to the first question. But I would recommend a simple setter instead of the special deserializer :

我也找不到 Hymanson 属性或对此的注释。所以我必须对第一个问题回答“否”。但我会推荐一个简单的 setter 而不是特殊的反序列化器:

public class Entity {

    @JsonDeserialize(contentAs = HomeImpl.class)
    private Collection<Home> homes = new ArrayList<>();

    public void setHomes(List<Home> homes) {
        if (homes != null)
            this.homes = homes;
    }
}

This is generic as it only uses the Homeinterface instead of HomeImpl. You don't need @JsonPropertyas Hymanson will associate setHomesand homes.

这是通用的,因为它只使用Home接口而不是HomeImpl. 您不需要,@JsonProperty因为Hyman逊会关联setHomeshomes

回答by Mike Partridge

As of Hymanson 2.9, it looks like null-handling for specific properties can be configured with @JsonSetter, for example:

从 Hymanson 2.9 开始,似乎可以使用 配置特定属性的空处理@JsonSetter,例如:

@JsonSetter(nulls = Nulls.AS_EMPTY)
public void setStrings(List<String> strings) {
    this.strings = strings;
}

Similar configuration can also be applied globally for collections:

类似的配置也可以全局应用于集合:

ObjectMapper mapper = objectMapperBuilder()
    .changeDefaultNullHandling(n -> n.withContentNulls(Nulls.AS_EMPTY))
    .build();

Or by type:

或按类型:

ObjectMapper mapper = objectMapperBuilder()
    .withConfigOverride(List.class,
        o -> o.setNullHandling(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY)))
    .build();

I haven't been able to try the feature out, so this is based on the feature discussionand examination of unit tests. YMMV.

我一直无法试用该功能,因此这是基于对单元测试功能讨论和检查。天啊。

回答by Malik Atalla

What worked for me was simply to remove the setter and make the attribute final. Hymanson 2 will then use the getter to modify the list.

对我有用的只是删除 setter 并使属性最终化。Hymanson 2 然后将使用 getter 来修改列表。

public class Entity {

  @JsonProperty(value="homes")
  @JsonDeserialize(as=HashSet.class, contentAs=HomeImpl.class)
  private final Collection<Home> homes = new ArrayList<Home>();

  public List<Home> getHomes() {
     return homes;
  }
}

The responsible feature is USE_GETTERS_AS_SETTERS which is turned on by default: https://github.com/FasterXML/Hymanson-databind/wiki/Mapper-Features

负责的功能是 USE_GETTERS_AS_SETTERS,默认开启:https: //github.com/FasterXML/Hymanson-databind/wiki/Mapper-Features