Search code examples
javajacksonjson-deserialization

Jackson @JsonDeserialize not working on field


@JsonDeserialize not working on field.

Using Spring Boot 2.

Code:

@Data
public class EventOddPOJO {

    @JsonProperty("id")
    private String id;

    @JsonDeserialize(using = EventOddPartDeserializer.class)
    private String part;

    @JsonProperty("ss")
    private String score;

    @JsonDeserialize(using = EventOddMinuteDeserializer.class)
    private String minute;

    @JsonDeserialize(using = EventOddSecondDeserializer.class)
    private String second;

    @JsonProperty("over_od")
    private String overOd;

    @JsonProperty("home_od")
    private String homeOd;

    @JsonProperty("draw_od")
    private String drawOd;

    @JsonProperty("away_od")
    private String awayOd;

    @JsonProperty("under_od")
    private String underOd;

    @JsonProperty("time_str")
    private String timeStr;

    @JsonProperty("add_time")
    private String addTime;

    @JsonProperty("handicap")
    private String handicap;

    public static class EventOddPartDeserializer extends StdDeserializer < String > {

        public EventOddPartDeserializer() {
            this(null);
        }

        public EventOddPartDeserializer(Class < ? > vc) {
            super(vc);
        }

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
        JsonProcessingException {

            String timeStr = p.getValueAsString("timeStr");

            if (Objects.isNull(timeStr)) {
                return "-";
            }

            return timeStr.split(" - ")[0];

        }
    }

    public static class EventOddMinuteDeserializer extends StdDeserializer < String > {

        public EventOddMinuteDeserializer() {
            this(null);
        }

        public EventOddMinuteDeserializer(Class < ? > vc) {
            super(vc);
        }

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
        JsonProcessingException {

            String timeStr = p.getValueAsString("timeStr");

            if (Objects.isNull(timeStr)) {
                return "-";
            }

            return timeStr.split(" - ")[1].split(":")[0];

        }
    }

    public static class EventOddSecondDeserializer extends StdDeserializer < String > {

        public EventOddSecondDeserializer() {
            this(null);
        }

        public EventOddSecondDeserializer(Class < ? > vc) {
            super(vc);
        }

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
        JsonProcessingException {

            String timeStr = p.getValueAsString("timeStr");

            if (Objects.isNull(timeStr)) {
                return "-";
            }

            return timeStr.split(" - ")[1].split(":")[1];

        }
    }

}

Json:

{
                    "id": "35008186",
                    "home_od": "1.952",
                    "handicap": "+3.5",
                    "away_od": "1.740",
                    "ss": "95:98",
                    "time_str": "4 - 04:08",
                    "add_time": "1603807890"
}

Solution

  • You can not use 3 different deserialisers at the same time for the same value in JSON payload. If you need to parse value into 3 different properties you can create new POJO and implement parsing in custom deserialiser.

    POJO-s:

    @Data
    class EventOddPOJO {
    
        @JsonProperty("id")
        private String id;
    
        @JsonProperty("ss")
        private String score;
    
        @JsonProperty("over_od")
        private String overOd;
    
        @JsonProperty("home_od")
        private String homeOd;
    
        @JsonProperty("draw_od")
        private String drawOd;
    
        @JsonProperty("away_od")
        private String awayOd;
    
        @JsonProperty("under_od")
        private String underOd;
    
        @JsonDeserialize(using = EventOddPartDeserializer.class)
        @JsonProperty("time_str")
        private EventOddPart timeStr;
    
        @JsonProperty("add_time")
        private String addTime;
    
        @JsonProperty("handicap")
        private String handicap;
    }
    
    @Data
    class EventOddPart {
        private String part;
        private String minute;
        private String second;
    }
    

    Custom deserialiser:

    class EventOddPartDeserializer extends JsonDeserializer<EventOddPart> {
    
        @Override
        public EventOddPart deserialize(JsonParser p, DeserializationContext ctxt) throws IOException{
            String timeStr = p.getValueAsString();
    
            EventOddPart eop = new EventOddPart();
            if (Objects.isNull(timeStr)) {
                return eop;
            }
            String[] parts = timeStr.split(" - ");
            String[] minSecs = parts[1].split(":");
            eop.setPart(parts[0]);
            eop.setMinute(minSecs[0]);
            eop.setSecond(minSecs[1]);
    
            return eop;
        }
    }