Search code examples
jacksonjson-deserializationobjectmapperfasterxmljackson-databind

Jackson - InvalidFormatException deserializing ENUM


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!


Solution

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