[英]Deserialization null value to enum with Jackson
I have problem with JSON deserialization and mapping it to enum.我对 JSON 反序列化并将其映射到枚举有问题。 I'm getting JSON from external API simillar to this two examples:
我从外部 API 获得 JSON 类似于这两个示例:
{
"someValue": null
}
{
"someValue": "exists"
}
I would like to map null values to some default enum value.我想将 map null 值设置为一些默认枚举值。
Model object Model object
SomeEnum someValue;
and enum class和枚举 class
public enum SomeEnum {
@JsonProperty("exists") EXISTS,
NONE;
}
For exists, value model class contains correct enum, but if I get null
from API, it is still null
in the model. For exists, value model class contains correct enum, but if I get
null
from API, it is still null
in the model.
I tried to create some method annotated by @JsonCreator
, creating own enum deserializer, using @JsonEnumDefaultValue
but none of these solutions work for me.我尝试创建一些由
@JsonCreator
注释的方法,创建自己的枚举反序列化器,使用@JsonEnumDefaultValue
但这些解决方案都不适合我。 Do anyone knows, how can I deserialize nulls to some default enum?有谁知道,我怎样才能将空值反序列化为一些默认枚举?
(Honestly, I wrote this code here without testing it, maybe you need to modify it a bit) (老实说,我在这里写了这段代码没有测试它,也许你需要稍微修改一下)
You can try to do something like following ( Enum
with a constructor and use @JsonCreator
):您可以尝试执行以下操作(带有构造函数的
Enum
并使用@JsonCreator
):
public enum SomeEnum {
EXISTS("exists"),
NONE(null);
private String value;
SomeEnum (String value) {
this.value = value;
}
@JsonCreator
public static SomeEnum fromValue(String value) {
for (SomeEnum someEnum : SomeEnum.values()) {
if (StringUtils.equalsIgnoreCase(someEnum.getValue(), value)) {
return someEnum;
}
}
throw new IllegalArgumentException("Unknown value " + value);
}
public String getValue () {
return value;
}
}
If it doesn't work, keep the above enum and try to make a custom converter (without Jackson) as following如果它不起作用,请保留上面的枚举并尝试制作一个自定义转换器(没有 Jackson),如下所示
If using spring boot You should put this binder in your controller如果使用 spring 引导您应该将此活页夹放入 controller
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(SomeEnum.class, new SomeEnumConverter());
}
The following is the custom converter to specify how you convert your input value to an enum value.以下是自定义转换器,用于指定如何将输入值转换为枚举值。
public class SomeEnumConverter extends PropertyEditorSupport {
@Override
public void setAsText(final String text) {
setValue(SomeEnum.fromValue(text));
}
}
Ok, so for now I solved this issue by creating custom enum deserializer.好的,所以现在我通过创建自定义枚举反序列化器解决了这个问题。
class SomeEnumDeserializer extends StdDeserializer<SomeEnum> {
SomeEnumDeserializer() {
super(SomeEnum.class);
}
@Override
public SomeEnum getNullValue(DeserializationContext ctxt) {
return SomeEnum.NONE;
}
@Override
public SomeEnum deserialize(JsonParser p, DeserializationContext ctxt) {
// implementation here
}
and registering it using @JsonDeserialize:并使用@JsonDeserialize 注册它:
@JsonDeserialize(using = SomeEnumDeserializer.class)
public enum SomeEnum {
// code
}
But still I'd prefer using something like @JsonProperty but for null, like @JsonNullProperty or something like this.但我仍然更喜欢使用@JsonProperty 之类的东西,但对于 null,例如@JsonNullProperty 或类似的东西。
Set the starting value of the field to the enum's value.将字段的起始值设置为枚举的值。
public class ExampleClass {
@JsonProperty("someValue") public SomeEnum someValue = SomeEnum.NONE;
public enum SomeEnum {
@JsonProperty("exists") EXISTS,
NONE;
}
}
However, this will mean if the value doesn't exist it will also be the same value as if null
was specified.但是,这意味着如果该值不存在,它也将与指定
null
时的值相同。 So these two JSON strings will produce the same POJO:所以这两个 JSON 字符串将产生相同的 POJO:
{}
{"someValue":null}
Not sure if that behavior is desired by you.不确定您是否需要这种行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.