yasson
yasson copied to clipboard
JsonbDateFormat deserialization fails
Check test:
`public class ParseDateTest {
public ParseDateTest() {
}
@Test
public void testDate() {
String json = "{\"date\":\"2018-03-05\"}";
Jsonb jsonb = JsonbBuilder.create();
Form form = jsonb.fromJson(json, Form.class);
System.out.println("date:" + form.getDate());
}
public static class Form {
@JsonbDateFormat(value = "yyyy-MM-dd")
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
}`
Throws excepction:
java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO,UTC resolved to 2018-03-05 of type java.time.format.Parsed
Yasson leverages java 8 Date/Time API for parsing dates.
java.util.Date is a time instant, while 2018-03-05 is not, since it is missing either the time zone or UTC offset and time of the day.
What version are you using? When I run your example against master I get:
javax.json.bind.JsonbException: Error parsing class java.util.Date from value: 2018-03-05. Check your @JsonbDateFormat has all time units for class java.util.Date type, or consider using org.eclipse.yasson.YassonProperties#ZERO_TIME_PARSE_DEFAULTING.
Which wraps the java.time.DateTimeException is meant to be self explanatory.
@Verdent when we will get a new version of Yassson in cetral?
Ok, con LocalDate works
public class ParseDateTest {
@Test
public void testDate() {
String json = "{\"date\":\"2018-03-05\"}";
Jsonb jsonb = JsonbBuilder.create();
Form form = jsonb.fromJson(json, Form.class);
System.out.println("date:" + form.getDate());
}
public static class Form {
@JsonbDateFormat(value = "yyyy-MM-dd")
private LocalDate date;
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
}
}
While I accept the argument on java.util.Date being a timestamp format, the JSON-B section 3.5.1 specification says:
Implementations MUST support deserialization of both ISO_DATE and ISO_DATE_TIME into java.util.Date, Calendar and GregorianCalendar instances.
Therefore there should be no exception when deserializing "2018-03-05" into java.util.Date at least when no date format annotation is present. But this appears not to be the case.
Are there any news about this issue? I have the same problem reported by @pdudits
I use LocalDate class instead. I no longer have problems, and it works very well
El jue., 9 may. 2019 a las 5:45, notarmara ([email protected]) escribió:
Are there any news about this issue? I have the same problem reported by @pdudits https://github.com/pdudits
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/eclipse-ee4j/yasson/issues/162#issuecomment-490853276, or mute the thread https://github.com/notifications/unsubscribe-auth/AIFZI2KZ7OZ62M65U37DD6TPUP6C3ANCNFSM4F3CBVDA .
-- Ing. Diego E. Silva Límaco
yes but in this way it's needed to change code implementation. I develop a framework and I wouldn't that developers who use it must change all their java.util.Date into LocaleDate. I can perform a workaround of course, such as custom deserializer/serializer, but I agree with @pdudits when he says that it should be no exception when deserializing "2018-03-05" into java.util.Date, at least when no date format annotation is present.
The issue still persists with 1.0.4 but works fine with LocalDate
While I accept the argument on
java.util.Datebeing a timestamp format, the JSON-B section 3.5.1 specification says:Implementations MUST support deserialization of both ISO_DATE and ISO_DATE_TIME into java.util.Date, Calendar and GregorianCalendar instances.
Therefore there should be no exception when deserializing
"2018-03-05"intojava.util.Dateat least when no date format annotation is present. But this appears not to be the case.
Your requirement is correct but it's related to default mapping. The JsonbDateFormat makes the context non default. May be there is an ambiguity in the Java EE 8 specifications?
I have the same issue when with zoned date-time, any updates on this yet?
A same error in glassfish (7.0.12) leads me to here. As https://github.com/eclipse-ee4j/yasson/issues/162#issuecomment-482566331 stated, the date format without time still not working in glassfish. And I believe the same happens in yasson since both are eclipse products...