0
votes

I am trying to deserialize a JSON string using jackson-databind Objectmapper which has a set of Enum attributes within it. This happens when an unknown attribute, which is not defined in the enum comes in the JSON. Please find below the object mapper configuration that I am using.

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    objectMapper.setSerializationInclusion(Include.NON_NULL);
   objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

But, deserialization (objectMapper.readValue(jsonText, .class);) throws this error. "Test" is the unknown attribute that comes in the JSON String to be deserialized.

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize Map key of type com..* from String "Test": not a valid representation, problem: (com.fasterxml.jackson.databind.exc.InvalidFormatException) Cannot deserialize Map key of type com.... from String "Test": not one of values excepted for Enum class: [ ]

Could someone please help me out with this issue?

Thanks!

1
As far as I know you will need a custom deserializer to ignore the unknow enum values.Franjavi
yea.. looks like. This has been reported to the jackson-databind github issues, so that it could be added as a fix in one of the upcoming releases.user1347244
This is linked to the jackson-databind issue posted here: github.com/FasterXML/jackson-databind/issues/1859user1347244

1 Answers

0
votes

This was finally fixed by writing a custom deserializer to filter out the enum values and setting them.