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!
On Monday, 11 December 2017 21:03:44 UTC-6, Remya 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.