7

I have problem with JSON deserialization and mapping it to enum. I'm getting JSON from external API simillar to this two examples:

{
 "someValue": null
}
{
 "someValue": "exists"
}

I would like to map null values to some default enum value.

Model object

SomeEnum someValue;

and enum 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.

I tried to create some method annotated by @JsonCreator, creating own enum deserializer, using @JsonEnumDefaultValue but none of these solutions work for me. Do anyone knows, how can I deserialize nulls to some default enum?

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Kacper Fleszar
  • 156
  • 1
  • 8

3 Answers3

0

(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):

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

If using spring boot You should put this binder in your 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));
    }
}
Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
0

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(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.

Kacper Fleszar
  • 156
  • 1
  • 8
0

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. So these two JSON strings will produce the same POJO:

{}
{"someValue":null}

Not sure if that behavior is desired by you.

Jack Cole
  • 1,528
  • 2
  • 19
  • 41