Jackson 防止枚举的空值反序列化

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

Jackson Prevent Null Deserialization of Enum

问题

我正在尝试理解Jackson JSON反序列化如何理解空值。如果我有一个将单个枚举映射到JSON的情况:

{
    "myEnum": null
}

映射到字段:

private myEnum enum;

并且myEnum如下所示,没有定义空值:

public enum myEnum {
   foo("foo"),
   bar("bar")
  // ...
}

Jackson是否提供一种原生方式来阻止将null识别为此枚举的有效值,还是此验证需要在代码中进行管理?

英文:

I'm trying to understand how Jackson JSON deserialization understand null values. If I have a JSON that maps a single enum:

{
    "myEnum": null
}

To a field

private myEnum enum;

and myEnum looks as follows with no null value defined:

public enum myEnum {
   foo("foo"),
   bar("bar")
  // ...
}

Does Jackson offer a native way to prevent null from being recognized as a valid value for this enum, or does this validation need to be managed in code?

答案1

得分: 1

Jackson会像对待其他类型的值一样,只会放置null。如果您想要验证,可以稍后进行。不应在反序列化过程中执行验证。

英文:

Like with all other types of value, Jackson will just put null. If you want to validate, you can do it later. You shouldn't do it during deserialization.

huangapple
  • 本文由 发表于 2023年6月15日 21:00:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482749.html
匿名

发表评论

匿名网友

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

确定