2

I'm trying to deserialize a time string to a Joda DateTime, so I defined this deserializer:

public class JsonTimeDeserializer<T extends DateTime> implements JsonDeserializer<T> {
    @Override
    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:mm");
        return json == null ? null : dtf.parseDateTime(json.getAsString());
    }
}

I'm getting:

required: T
Found org.joda.time.DateTime

I don't understand. T extends DateTime.

What am I doing wrong?

Thanks

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69

1 Answers1

2

Your first line should read

public class JsonTimeDeserializer implements JsonDeserializer<DateTime>

You don't need to create your own variable, just reference the existing one

When you extend a generic type and add a new type variable, you are making your type generic as well, which is not what you want in this case.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588