Java – cannot convert an empty string value to an enumeration

I am looking for a convenient solution to filter out fields pointing to empty string values before / during deserialization with Jackson (2.8):

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Guis {

public enum Context {
    SDS,NAVIGATION
}

public enum Layer {
    Library,Status,Drivingdata
}

// client code

 String s = "{\"context\":\"\",\"layer\":\"Drivingdata\"}";
 ObjectMapper mapper = new ObjectMapper();
 Guis o = mapper.readValue(s,Guis.class);

error

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type cq.speech.rsi.api.Guis$Context from String "": value not one of declared Enum instance names: [SDS NAVIGATION] at [Source: {"context":"","layer":"Drivingdata"}; line: 1,column: 12] (through reference chain: cq.speech.rsi.api.Guis["context"])

What else have I tried... Ah, this one

mapper.getDeserializationConfig().with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

The error still exists. Obviously, even Google search doesn't help much

edit

The set deserialization feature does not work as illustrated above For me, the final solution is to add this fragment:

mapper.configure(DeserializationFeature.READ_UNKNowN_ENUM_VALUES_AS_NULL,true)

Solution

You can enable the deserialization feature for the mapper READ_ UNKNowN_ ENUM_ VALUES_ AS_ Null, disabled by default Just caution against using it. It will treat all unknown including empty strings as null enumerations

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>