On Tue, Dec 12, 2017 at 8:11 AM, Remya <[email protected]> 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 +- > > Thanks! > > On Monday, 11 December 2017 23:35:52 UTC-6, Tatu Saloranta wrote: >> >> On Mon, Dec 11, 2017 at 8:37 PM, Remya <[email protected]> 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 +- >> >> > >> > Regards, >> > Remya >> > >> > On Monday, 11 December 2017 22:21:32 UTC-6, Tatu Saloranta wrote: >> >> >> >> 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 +- >> >> >> >> >> >> On Mon, Dec 11, 2017 at 7:03 PM, Remya <[email protected]> wrote: >> >>> >> >>> >> >>> 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! >> >>> >> >>> On Monday, 11 December 2017 17:37:34 UTC-6, Tatu Saloranta wrote: >> >>>> >> >>>> Ok, so .... what are classes involved? And input JSON? >> >>>> >> >>>> -+ Tatu +- >> >>>> >> >>>> On Mon, Dec 11, 2017 at 1:50 PM, Remya <[email protected]> wrote: >> >>>>> >> >>>>> 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! >> >>>>> >> >>>>> -- >> >>>>> 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 [email protected]. >> >>>>> To post to this group, send email to [email protected]. >> >>>>> For more options, visit https://groups.google.com/d/optout. >> >>>> >> >>>> >> >>> -- >> >>> 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 [email protected]. >> >>> To post to this group, send email to [email protected]. >> >>> For more options, visit https://groups.google.com/d/optout. >> >> >> >> >> > -- >> > 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 [email protected]. >> > To post to this group, send email to [email protected]. >> > For more options, visit https://groups.google.com/d/optout. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
