'JAVA - JSON Serialize a list of enum to a class

I have this enum:

public enum Days implements Serializable {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    ...
}

I want to use it in a Store class:

 public class Store{
      private integer id_store;
      private String name;
      private Days days_visit;
}

The thing is, the days_visit needs to be an array because it can be more than one day; in the database side, the Days type is a days type:

CREATE TYPE schema.days AS ENUM
   (MONDAY,
    TUESDAY,
    WEDNESDAY,
    ...);

And the table Stores has an array of ´days´

days_visit schema.days[],

How can I serialize this in JSON? the JSON I tried is:

{"id_store":"1", "name":"The Store", "days_visit":["MONDAY", "FRIDAY"]}

But I get this error:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.domain.Days
    out of START_ARRAY token
    at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@5c2a7de1; line: 1, column: 168] (through reference chain:
    org.Stores["days_visit"])

If I declare the days_visit like this in my class:

 public class Store{
      private integer id_store;
      private String name;
      private Days[] days_visit;
}

I get this error when deploying in Wildfly:

[2020-01-28 11:42:09,970] Artifact app:war: Error during artifact deployment. See server log for details.
    [2020-01-28 11:42:09,970] Artifact app:war: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"app.war#org\"" => "javax.persistence.PersistenceException: [PersistenceUnit: org_app] Unable to build Hibernate SessionFactory
        Caused by: javax.persistence.PersistenceException: [PersistenceUnit: org.app] Unable to build Hibernate SessionFactory
        Caused by: org.hibernate.MappingException: Unable to instantiate custom type: org.hibernate.type.EnumType
        Caused by: java.lang.ClassCastException: class [Lpy.org.app.domain.Days;"}}

Is there something I'm missing?



Solution 1:[1]

The issue seems to be deserializing the JSON.

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.domain.Days

Jackson is unable to deserialize the enum. You can write a custom deserializer method and annotate it with @JsonCreator.

Tweak the below logic as per your requirement/constraints.

public enum Days {
    MONDAY, TUESDAY, WEDNESDAY;
    static Map<String, Days> daysLookup = new HashMap<>();

    static {
        daysLookup.put("MONDAY", MONDAY);
        daysLookup.put("TUESDAY", TUESDAY);
        daysLookup.put("WEDNESDAY", WEDNESDAY);
    }


    @JsonCreator
    public static Days[] create(@JsonProperty("days_visit") String[] days) {
        Days[] daysVisit = new Days[days.length];
        for (int i = 0; i < days.length; i++) {
            daysVisit[i] = daysLookup.get(days[i]);
        }
        return daysVisit;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1