Jackson. Deserialeze lists with different generic types

corporal_m

I want deserialize List<Contact> with custom deserialezer using jackson library. List<Document> and List<Good> should deserialize by default.

There is my class

public class Application implements Serializable {
    //other fields

    private List<Document> documents;
    private List<Contact> contacts;
    private List<Good> goods;
}

But I can't change it to add annotations. So I use mixin

public abstract class ApplicationMixin {
    @JsonProperty("contactId")
    private List<Contact> contacts;
}

Custom deserializer:

@JsonComponent
public class ContactsDeserializer extends JsonDeserializer<List<Contact>> {
    @Override
    public List<Contact> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        List<Contact> contacts = new ArrayList<>();

        //some logic

       return contacts;
   }
}

And during deserialization all lists try to use this custom deserializer. But I want it only for contacts. What can I do for this?

Mafor

The observed behavior is caused by the type erasure. On runtime, Jackson cannot tell the difference between List<Contact>, List<Document> or any other List, so it applies the ContactsDeserializer to all of them. You will need to be more precise about what the deserializer should be applied to.

You can try the following:

  1. remove @JsonComponent annotation from the ContactsDeserializer
  2. in the ApplicationMixin, add @JsonDeserialize(using = ContactsDeserializer.class) to the contacts field.
public abstract class ApplicationMixin {
    @JsonProperty("contactId")
    @JsonDeserialize(using = ContactsDeserializer.class)
    private List<Contact> contacts;
}

Alternatively, you might consider creating a custom deserializer for the Contact class instead of the List<Contact>. If it's possible depends on the input JSON format.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic Interface with different types

From Dev

How to iterate through two generic lists with different types one item after another in java?

From Dev

How to iterate through two generic lists with different types one item after another in java?

From Dev

Method that receives different generic lists doesn't work due to incompatible types

From Dev

JavaFX bindContentBidirectional of different types of Lists

From Dev

Merge two Lists of different types

From Dev

Generic delegate with different retun types

From Java

Deserialising different types to single field using Jackson

From Dev

Deserializing attributes of same name but different types in Jackson?

From Dev

Deserializing attributes of same name but different types in Jackson?

From Dev

Merge 2 Lists of different types using LinQ

From Dev

Python - "Joining" list of lists of different types

From Dev

Adding Two Lists of Different Types Into Another List

From Dev

Merge 2 Lists of different types using LinQ

From Dev

Sort a list of lists in Python with different data types

From Dev

Different return value types in implementation of generic methods

From Dev

Generic method that can return different types

From Dev

Fortran: Handling types with different kind in generic procedures

From Dev

Apply Generic logic for different types in Java

From Dev

Two different generic types as result and parameter

From Dev

Java: Generic collection for holding primitives of different types

From Dev

Generic method to map objects of different types

From Dev

Storing a list of different generic types in a class

From Dev

Constraining generic parameters to be different types in Java

From Dev

overriding with different return types (generic collections)

From Dev

Two different generic types as result and parameter

From Dev

Generic method that can return different types

From Dev

Generic Repository with different databases, entity types and dbcontext

From Dev

Implement Same Function for different Generic Types with Reflection

Related Related

  1. 1

    Generic Interface with different types

  2. 2

    How to iterate through two generic lists with different types one item after another in java?

  3. 3

    How to iterate through two generic lists with different types one item after another in java?

  4. 4

    Method that receives different generic lists doesn't work due to incompatible types

  5. 5

    JavaFX bindContentBidirectional of different types of Lists

  6. 6

    Merge two Lists of different types

  7. 7

    Generic delegate with different retun types

  8. 8

    Deserialising different types to single field using Jackson

  9. 9

    Deserializing attributes of same name but different types in Jackson?

  10. 10

    Deserializing attributes of same name but different types in Jackson?

  11. 11

    Merge 2 Lists of different types using LinQ

  12. 12

    Python - "Joining" list of lists of different types

  13. 13

    Adding Two Lists of Different Types Into Another List

  14. 14

    Merge 2 Lists of different types using LinQ

  15. 15

    Sort a list of lists in Python with different data types

  16. 16

    Different return value types in implementation of generic methods

  17. 17

    Generic method that can return different types

  18. 18

    Fortran: Handling types with different kind in generic procedures

  19. 19

    Apply Generic logic for different types in Java

  20. 20

    Two different generic types as result and parameter

  21. 21

    Java: Generic collection for holding primitives of different types

  22. 22

    Generic method to map objects of different types

  23. 23

    Storing a list of different generic types in a class

  24. 24

    Constraining generic parameters to be different types in Java

  25. 25

    overriding with different return types (generic collections)

  26. 26

    Two different generic types as result and parameter

  27. 27

    Generic method that can return different types

  28. 28

    Generic Repository with different databases, entity types and dbcontext

  29. 29

    Implement Same Function for different Generic Types with Reflection

HotTag

Archive