joda.time.DateTime deserialization error
Asked Answered
H

1

10

I tried to deserialize a class with a DateTime as attibute:

import org.joda.time.DateTime;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer;
import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer;

class MyClass {

    private DateTime alertTimestamp;

    private String name;

    @JsonSerialize(using = DateTimeSerializer.class)
    public DateTime getAlertTimestamp() {
        return alertTimestamp;
    }

    @JsonDeserialize(using = DateTimeDeserializer.class)
    public void setAlertTimestamp(DateTime now) {
        this.alertTimestamp = now;
    }

    //...
}

But when I try tro deserialize, I have this exception:

com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has no default (no arg) constructor

I use that to deserialize:

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonData, MyClass.class);

And an example of my jsonData:

{    
    "name":"test",
    "alertTimestamp":       {"year":2014,"era":1,"dayOfMonth":24,"dayOfWeek":1,"dayOfYear":83,"weekOfWeekyear":13,"weekyear":2014,"monthOfYear":3,"yearOfEra":2014,"yearOfCentury":14,"centuryOfEra":20,"millisOfSecond":232,"millisOfDay":45143232,"secondOfMinute":23,"secondOfDay":45143,"minuteOfHour":32,"minuteOfDay":752,"hourOfDay":12,"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"America/Los_Angeles"},"fixed":false,"id":"America/Los_Angeles"},"millis":1395689543232,"chronology":{"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"America/Los_Angeles"},"fixed":false,"id":"America/Los_Angeles"}},"afterNow":false,"beforeNow":false,"equalNow":true}
}
Hurless answered 25/3, 2014 at 18:32 Comment(7)
Can you clarify timestamp vs alertTimestamp?Few
Sorry it was a mistakeHurless
What JSON generator generated the JSON in jsonData? DateTimeSerializer does not serialize into that format.Few
According to this source the class DateTimeDeserializer has a public no-arg constructor. This arises the question if you have the proper import or appropriate jar-version?Glisten
@MenoHochschild Version 2.3.0 doesn't.Few
The data has been generated before I used the DataTimeSerialiser. I generated new data, now I have "alertTimestamp":1395775173517 but the exception is still there.Hurless
I don't have this constructor in my DateTimeDeserializer sourceHurless
F
12

@JsonDeserialize expects a JsonDeserializer with a no-arg constructor. The most recent version of DateTimeDeserializer does not have such a constructor.

If you've fixed the format, ie. alertTimestamp should just be a timestamp, then you could simply register the JodaModule with the ObjectMapper. It will use DateTimeDeserializer internally for DateTime fields. You can get rid of the @JsonDeserialize annotations.

mapper.registerModule(new JodaModule());

You'll need to add the jackson-datatype-joda library.

Few answered 25/3, 2014 at 19:59 Comment(6)
Could you please provide an example?Plagal
@arcone What would you like an example of?Few
I figured it out - it was a Spring response issue with DateTime.Plagal
I've encountered a bug in GlassFish that refuses me to register the JodaModule :-( Is there another way to apply the deserialization method?Entirely
@Entirely You'll need to register a custom deserializer for DateTime fields.Few
I've found out that it's possible to use an older version of the library jackson-datatype-joda (v2.4.6)Entirely

© 2022 - 2024 — McMap. All rights reserved.