5

I have a class which I'm trying to deserialise some JSON into. One of the properties has a custom converter attached via the @JsonDeserialize annotation. The deserialisation works fine when the class is written with setters and the annotations on the setters like this:

public class Rates extends AbstractResponse {

    private Date effectiveDate;
    private List<Rate> rates;

    public Rates(Date effectiveDate, List<Rate> rates) {
        this.effectiveDate = effectiveDate;
        this.rates = rates;
    }

    public Date getEffectiveDate() {
        return this.effectiveDate;
    }

    @JsonProperty("timestamp")
    public void setEffectiveDate(Date effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    @JsonDeserialize(converter = RatesTableConverter.class)
    public void setRates(List<Rate> rates) {
        this.rates = rates;
    }

    public List<Rate> getRates() {
        return this.rates;
    }

}

But when I re-write it to be like this:

public class Rates extends AbstractResponse {

    private final Date effectiveDate;
    private final List<Rate> rates;

    @JsonCreator
    public Rates(
            @JsonProperty("timestamp") Date effectiveDate,
            @JsonProperty("rates") 
            @JsonDeserialize(converter = RatesTableConverter.class) List<Rate> rates) {
        this.effectiveDate = effectiveDate;
        this.rates = rates;
    }

    public Date getEffectiveDate() {
        return this.effectiveDate;
    }

    public List<Rate> getRates() {
        return this.rates;
    }

}

I get this error:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

I've messed around with a number of ideas and cannot get it to work. It appears that when used on a constructor, that the deserialiser is not being checked correctly.

Any ideas what I've missed?

1
  • Interesting problem! I tried to recreate it but got a different error. However, when I used a JsonDeserializer<List<Rate>> and the annotation @JsonDeserialize(using = RateDeserializer.class) instead I got it to work.
    – wassgren
    Jan 21, 2015 at 7:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.