question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonDeserialize not working on field

See original GitHub issue

@JsonDeserialize not working on field.

Using Spring Boot 2 and Jackson 2.11.3.

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"
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
lukasdocommented, Dec 17, 2021

I had the same issue using spring boot version 2.5.6. I resolved it by adding a no arguments constructor to the POJO class.

The primary method for deserializing objects of the BeanDeserializer.java class the is using a flag called _vanillaProcessing. In my case without a no arguments constructor, _vanillaProcessing was disabled and another deserializer was used. However with a no arg constructor _vanillaProcessing was enabled and my custom JsonDeserialize was triggered.

0reactions
marciodevelopmentcommented, Feb 25, 2021

I´m using a spring boot version 2.3.9.RELEASE and i´m have the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson @JsonDeserialize not working on field - Stack Overflow
Using Spring Boot 2. Code: @Data public class EventOddPOJO { @JsonProperty("id") private String id; ...
Read more >
JSON Deserialize (Values are not assigning) - OutSystems
I think the problem is that your JSON is an array (of 1), so your Data Type property on the JSONDeserialize shoud be...
Read more >
Getting Started with Custom Deserialization in Jackson
This quick tutorial will illustrate how to use Jackson 2 to deserialize JSON using a custom Deserializer.
Read more >
JsonDeserialize (jackson-databind 2.8.0 API) - FasterXML
Deserializer class to use for deserializing Map keys of annotated property. Can only be used on instances (methods, fields, constructors), and not value...
Read more >
Java JSON deserialization problems with the Jackson ... - Snyk
Without any annotations, the Jackson ObjectMapper uses reflection to do the POJO mapping. Because of the reflection, it works on all fields ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found