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.