How to enforce ACCEPT_SINGLE_VALUE_AS_ARRAY in jackson's deserialization process using annotation

Vahid

Is there a way to use annotation on a List property in a class to use ACCEPT_SINGLE_VALUE_AS_ARRAY in Jackson? I'm using Spring and getting the below exception

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

Assume I have a class as below:

public class MyClass {

    private List < String > value;
}

And my JSON structures are as below:

case 1:

[{"operator": "in", "value": ["Active"], "property": "status"}]

case 2:

[{"operator": "like", "value": "aba", "property": "desc"}]

What annotation should I use to let the framework know I want these 2 cases to be treated the same when deserializing.

UPDATE: I moved the updates to an answer in this post for more clarity.

Vahid

I'm just answering my own question for clarity. One of the answers was to upgrade to the higher version so that I can use annotations. I cannot do it due to dependency restrictions of my project.

As a result, based on Michal Foksa answer I solved my problem by creating a custom deserializer. Its as below:

On my property:

@JsonDeserialize(using = CustomStringDeserializer.class)
private List<String> value;

And my Deserializer:

public class CustomStringDeserializer extends JsonDeserializer<List<String>>{

    @Override
    public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature. ACCEPT_SINGLE_VALUE_AS_ARRAY);
        return mapper.readValue(p, List.class);
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to enforce ACCEPT_SINGLE_VALUE_AS_ARRAY in jackson's deserialization process using annotation

From Dev

Annotation - Enforce a square using imrect

From Dev

Annotation - Enforce a square using imrect

From Dev

How To Use Jackson's @JsonIdentityInfo for Deserialization of directed Graphs?

From Dev

Asymmetric serialization and deserialization using Jackson

From Dev

Custom deserialization of List using Jackson

From Dev

Using Scala Jackson for JSON deserialization?

From Dev

How to do a partial deserialization with Jackson

From Dev

How to enforce a single line using Regex?

From Dev

Is Jackson's @JsonSubTypes still necessary for polymorphic deserialization?

From Dev

How to enforce the correct behavior for the squid:S1161 rule: "@Override" annotation should be used on any method

From Dev

Deserialization of map with integer keys using Jackson

From Dev

Custom JSON serialization/deserialization using Jackson

From Dev

XML deserialization to POJO using Jackson XmlMapper

From Dev

Custom JSON serialization/deserialization using Jackson

From Dev

Deserialization with Jackson

From Dev

Custom JSON Deserialization in Jackson, exclude invalide array

From Dev

Deserialization of JavaScript array to Java LinkedHashSet using Jackson and Spring doesn't remove duplicates

From Dev

Jackson - deserialize using Builder without annotation

From Dev

Jackson's polymorphic serialization / deserialization AND custom serializer / deserializer

From Java

Jackson XML deserialization skips field when using multiple useWrapping = false

From Dev

Custom JSON deserialization only if certain field is present (using Jackson)

From Dev

XML deserialization using jackson-dataformat-xml custom root element?

From Dev

JSON Deserialization Joda Money using Jackson ObjectMapper causes exception

From Dev

Jackson deserialization fails after serializing an object using writeValueAsString

From Dev

Custom JSON deserialization only if certain field is present (using Jackson)

From Dev

Group list of objects in a list attribute during deserialization using Jackson

From Dev

Spring+Jackson: How to set visibility of a class without using @JsonAutoDetect annotation

From Dev

How convert an annotation (all its diff properties) into a JSON object using Jackson/ObjectMapper?

Related Related

  1. 1

    How to enforce ACCEPT_SINGLE_VALUE_AS_ARRAY in jackson's deserialization process using annotation

  2. 2

    Annotation - Enforce a square using imrect

  3. 3

    Annotation - Enforce a square using imrect

  4. 4

    How To Use Jackson's @JsonIdentityInfo for Deserialization of directed Graphs?

  5. 5

    Asymmetric serialization and deserialization using Jackson

  6. 6

    Custom deserialization of List using Jackson

  7. 7

    Using Scala Jackson for JSON deserialization?

  8. 8

    How to do a partial deserialization with Jackson

  9. 9

    How to enforce a single line using Regex?

  10. 10

    Is Jackson's @JsonSubTypes still necessary for polymorphic deserialization?

  11. 11

    How to enforce the correct behavior for the squid:S1161 rule: "@Override" annotation should be used on any method

  12. 12

    Deserialization of map with integer keys using Jackson

  13. 13

    Custom JSON serialization/deserialization using Jackson

  14. 14

    XML deserialization to POJO using Jackson XmlMapper

  15. 15

    Custom JSON serialization/deserialization using Jackson

  16. 16

    Deserialization with Jackson

  17. 17

    Custom JSON Deserialization in Jackson, exclude invalide array

  18. 18

    Deserialization of JavaScript array to Java LinkedHashSet using Jackson and Spring doesn't remove duplicates

  19. 19

    Jackson - deserialize using Builder without annotation

  20. 20

    Jackson's polymorphic serialization / deserialization AND custom serializer / deserializer

  21. 21

    Jackson XML deserialization skips field when using multiple useWrapping = false

  22. 22

    Custom JSON deserialization only if certain field is present (using Jackson)

  23. 23

    XML deserialization using jackson-dataformat-xml custom root element?

  24. 24

    JSON Deserialization Joda Money using Jackson ObjectMapper causes exception

  25. 25

    Jackson deserialization fails after serializing an object using writeValueAsString

  26. 26

    Custom JSON deserialization only if certain field is present (using Jackson)

  27. 27

    Group list of objects in a list attribute during deserialization using Jackson

  28. 28

    Spring+Jackson: How to set visibility of a class without using @JsonAutoDetect annotation

  29. 29

    How convert an annotation (all its diff properties) into a JSON object using Jackson/ObjectMapper?

HotTag

Archive