I have a JSON string that I want to deserilizie using Gson - {"Id":3,"Title":"Roskilde","Description":"Famous Danske festival","StartingTime":"2016-06-12T00:00:00","Duration":"02:02:00"}
The error I get when I try to deserilize the Duration field:
Unparseable date: "02:02:00"
The deserilizer(my idea was to add two possible formats of date deserialization):
Gson gSon= new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()).create();
private static final String[] DATE_FORMATS = new String[] {
"yyyy-MM-dd'T'HH:mm:ss",
"HH:mm:ss"
};
private class DateDeserializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}
}
and my Event Class(as you can see "Duration" is not of a type date - it's of the type Time - what should I do to make the deserilizer read Duration as type time not date?
private int Id;
private String Title;
private String Description;
private Date StartingTime;
private Time Duration;
public Event(int id, String title,String description, String place, Date startingTime, Time duration)
{
this.Id = id;
this.Description = description;
this.Title = title;
this.StartingTime = startingTime;
this.Duration = duration;
}
Add another class for Time deserialization - make sure you convert the return value to "Time". (Also see: cast a String to sql time)
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
private static final String TIME_FORMAT = "HH:mm:ss";
private class DateDeserializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
try {
return new SimpleDateFormat(DATE_FORMAT, Locale.US).parse(jsonElement.getAsString());
} catch (ParseException e) {
}
throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + DATE_FORMAT);
}
}
private class TimeDeserializer implements JsonDeserializer<Time> {
@Override
public Time deserialize(JsonElement jsonElement, Type typeOF,
JsonDeserializationContext context) throws JsonParseException {
try {
String s = jsonElement.getAsString();
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.US);
sdf.parse(s);
long ms = sdf.parse(s).getTime();
Time t = new Time(ms);
return t;
} catch (ParseException e) {
}
throw new JsonParseException("Unparseable time: \"" + jsonElement.getAsString()
+ "\". Supported formats: " + TIME_FORMAT);
}
}
Register both classes:
GsonBuilder gSonBuilder= new GsonBuilder();
gSonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
gSonBuilder.registerTypeAdapter(Time.class, new TimeDeserializer());
Gson gSon = gSonBuilder.create();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With