Jackson的@JsonDeserialize在字段上不起作用。

huangapple go评论103阅读模式
英文:

Jackson @JsonDeserialize not working on field

问题

@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"
}
英文:

@JsonDeserialize not working on field.

Using Spring Boot 2.

Code:

@Data
public class EventOddPOJO {

    @JsonProperty(&quot;id&quot;)
    private String id;

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

    @JsonProperty(&quot;ss&quot;)
    private String score;

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

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

    @JsonProperty(&quot;over_od&quot;)
    private String overOd;

    @JsonProperty(&quot;home_od&quot;)
    private String homeOd;

    @JsonProperty(&quot;draw_od&quot;)
    private String drawOd;

    @JsonProperty(&quot;away_od&quot;)
    private String awayOd;

    @JsonProperty(&quot;under_od&quot;)
    private String underOd;

    @JsonProperty(&quot;time_str&quot;)
    private String timeStr;

    @JsonProperty(&quot;add_time&quot;)
    private String addTime;

    @JsonProperty(&quot;handicap&quot;)
    private String handicap;

    public static class EventOddPartDeserializer extends StdDeserializer &lt; String &gt; {

        public EventOddPartDeserializer() {
            this(null);
        }

        public EventOddPartDeserializer(Class &lt; ? &gt; vc) {
            super(vc);
        }

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

            String timeStr = p.getValueAsString(&quot;timeStr&quot;);

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

            return timeStr.split(&quot; - &quot;)[0];

        }
    }

    public static class EventOddMinuteDeserializer extends StdDeserializer &lt; String &gt; {

        public EventOddMinuteDeserializer() {
            this(null);
        }

        public EventOddMinuteDeserializer(Class &lt; ? &gt; vc) {
            super(vc);
        }

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

            String timeStr = p.getValueAsString(&quot;timeStr&quot;);

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

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

        }
    }

    public static class EventOddSecondDeserializer extends StdDeserializer &lt; String &gt; {

        public EventOddSecondDeserializer() {
            this(null);
        }

        public EventOddSecondDeserializer(Class &lt; ? &gt; vc) {
            super(vc);
        }

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

            String timeStr = p.getValueAsString(&quot;timeStr&quot;);

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

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

        }
    }

}

Json:

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

答案1

得分: 1

以下是翻译好的内容:

不能同时为JSON负载中的同一个值使用3种不同的反序列化程序。如果您需要将值解析为3个不同的属性,可以创建新的POJO并在自定义反序列化程序中实现解析。

POJO类:

@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;
}

自定义反序列化程序:

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;
    }
}
英文:

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(&quot;id&quot;)
private String id;
@JsonProperty(&quot;ss&quot;)
private String score;
@JsonProperty(&quot;over_od&quot;)
private String overOd;
@JsonProperty(&quot;home_od&quot;)
private String homeOd;
@JsonProperty(&quot;draw_od&quot;)
private String drawOd;
@JsonProperty(&quot;away_od&quot;)
private String awayOd;
@JsonProperty(&quot;under_od&quot;)
private String underOd;
@JsonDeserialize(using = EventOddPartDeserializer.class)
@JsonProperty(&quot;time_str&quot;)
private EventOddPart timeStr;
@JsonProperty(&quot;add_time&quot;)
private String addTime;
@JsonProperty(&quot;handicap&quot;)
private String handicap;
}
@Data
class EventOddPart {
private String part;
private String minute;
private String second;
}

Custom deserialiser:

class EventOddPartDeserializer extends JsonDeserializer&lt;EventOddPart&gt; {
@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(&quot; - &quot;);
String[] minSecs = parts[1].split(&quot;:&quot;);
eop.setPart(parts[0]);
eop.setMinute(minSecs[0]);
eop.setSecond(minSecs[1]);
return eop;
}
}

huangapple
  • 本文由 发表于 2020年10月27日 23:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64557297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定