英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论