Jackson - InvalidFormatException deserializing ENUM

8,551 views
Skip to first unread message

Remya

unread,
Dec 11, 2017, 4:56:10 PM12/11/17
to jackson-user

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!

Tatu Saloranta

unread,
Dec 11, 2017, 6:37:34 PM12/11/17
to jackson-user
Ok, so .... what are classes involved? And input JSON?

-+ Tatu +-

--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscribe@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Remya

unread,
Dec 11, 2017, 10:03:44 PM12/11/17
to jackson-user

Sample of JSON String would look like this.

String jsonText="{\"cartId\":\"31028\",\"userId\":\"106784\",\"attributes\":{\"count\":\"1\",\"amount\":\"10\",\"email\":\"N\",\"Test\":\"No\",\"phone\":\"N\"}}";

The JSON maps to a Cart class, which holds these attributes as a Map. 

public class Cart {

  private String cartId;
  private String userId;
  private Map<Attributes,String> attributes;

}

Attributes map to the Enum, as given below.

public enum Attributes {
  Count(0),
  Amount(1),
  Email(2),
  Phone(3);

  private final int value;

  private Attributes(int value) {
    this.value = value;
  }

  public int getValue() {
    return value;
  }

  public static Attributes findByValue(int value) { 
    switch (value) {
      case 0:
        return Count;
      case 1:
        return Amount;
      case 2:
        return Email;
      case 3:
        return Phone;
      default:
        return null;
    }
  }
}

Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.

Remya

unread,
Dec 11, 2017, 10:44:55 PM12/11/17
to jackson-user

In between, also tried adding this configuration - objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);

This resulted in the Unknown attribute key to be deserialiezed as null. (i.e., instead of Test=No, it gets deserialized as null=No)

My intention is to ignore this attribute completely during deserialization. Please let me know if that would be possible by any means. 

Thanks!

Tatu Saloranta

unread,
Dec 11, 2017, 11:21:32 PM12/11/17
to jackson-user
In this case enum names do not match wrt casing? ("count" key, but enum `Count`; nor is there enum `Test` defined)
So it seems to me exception is due to simple mismatch between keys used, names of Enums.

-+ Tatu +-


To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscribe@googlegroups.com.

Remya

unread,
Dec 11, 2017, 11:37:23 PM12/11/17
to jackson-user

Thanks for the response.

That was just a sample that I posted. The cases do match and yes, my requirement is when a new attribute - say, 'Test' comes in the Json, I want the mapper to ignore that field and proceed with de-serializing the remaining fields that has a match in the Enum, rather than throwing the exception.

What should I do, so that it ignores the attributes which does not have an entry in the Enum?

Regards,
Remya

Tatu Saloranta

unread,
Dec 12, 2017, 12:35:52 AM12/12/17
to jackson-user
On Mon, Dec 11, 2017 at 8:37 PM, Remya <rem...@gmail.com> wrote:
>
> Thanks for the response.
>
> That was just a sample that I posted. The cases do match and yes, my
> requirement is when a new attribute - say, 'Test' comes in the Json, I want
> the mapper to ignore that field and proceed with de-serializing the
> remaining fields that has a match in the Enum, rather than throwing the
> exception.
>
> What should I do, so that it ignores the attributes which does not have an
> entry in the Enum?

You probably want to enable one of following `DeserializationFeature`s:

* `READ_UNKNOWN_ENUM_VALUES_AS_NULL`
* `READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE` (uses Enum value
annotated with `com.fasterxml.jackson.annotation.JsonEnumDefaultValue`)

-+ Tatu +-

Remya

unread,
Dec 12, 2017, 11:11:55 AM12/12/17
to jackson-user
Hi,

As I mentioned in one of my earlier responses, had also tried adding this configuration -
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);

This resulted in the Unknown attribute KEY to be deserialized as "null". (i.e., instead of Test=No, it gets deserialized as null=No)

My intention is to ignore this attribute completely during deserialization. Please let me know if that would be possible by any means.

Meanwhile, would try READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE

Thanks!

Tatu Saloranta

unread,
Dec 12, 2017, 6:03:47 PM12/12/17
to jackson-user
On Tue, Dec 12, 2017 at 8:11 AM, Remya <rem...@gmail.com> wrote:
> Hi,
>
> As I mentioned in one of my earlier responses, had also tried adding this
> configuration -
> objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL,
> true);
>
> This resulted in the Unknown attribute KEY to be deserialized as "null".
> (i.e., instead of Test=No, it gets deserialized as null=No)
>
> My intention is to ignore this attribute completely during deserialization.
> Please let me know if that would be possible by any means.
>
> Meanwhile, would try READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE

Ah. The challenge here is that it is not an Enum-valued property, but
EnumMap (or, equivalently, EnumSet) valued
property, meaning that deserializer for the Map would need to be aware
of the special handling.
And for general Maps there just isn't any way to declare that there
are invalid entries that are to be ignored --
invalid entries are typicallt hard failures.

However, it seems like a reasonable improvement idea to allow
specialized handling, since I think there is
a dedicated serializer (due to special requirements for constructing EnumMap).

Could you file am issue for `jackson-databind` for requesting handling
of unknown/unmapped Enum keys,
wrt `DeserializationFeature`s discussed here?
To me it seems reasonable that "as null" would result in "ignore"
(many Map types do not allow `null` keys,
specifically EnumMap doesn't). This could even be implemented in a
patch for 2.9 (if I have time) since it
does not require API changes.

-+ Tatu +-

Remya

unread,
Dec 13, 2017, 12:45:01 AM12/13/17
to jackson-user

Thanks much!

I would post this as an issue.

Regards,
Remya

Remya

unread,
Dec 13, 2017, 11:50:05 PM12/13/17
to jackson-user
Hi,

Please let me know when you have the fix available. 

Thanks,
Remya

Tatu Saloranta

unread,
Dec 14, 2017, 6:35:32 PM12/14/17
to jackson-user
Thank you!

If and when there is an update, it will be added in that issue, and
you should be notified by github (as per settings
you have for your account; default will send an email on issues you
have created I think).

-+ Tatu +-

Remya

unread,
Dec 15, 2017, 11:26:05 AM12/15/17
to jackson-user
Thank you!

Remya

unread,
Jan 3, 2018, 3:21:22 PM1/3/18
to jackson-user
Hi,

Any update on this fix? It would be really helpful if we get this in any of your newer versions. As of now, we are doing a workaround with the Map implementation to get this fixed.

Regards,
Remya

Tatu Saloranta

unread,
Jan 3, 2018, 3:43:46 PM1/3/18
to jackson-user
No updates as of yet -- recovering from christmas holidays. As per my
earlier note, updates will be reflected on issue itself.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages