yasson icon indicating copy to clipboard operation
yasson copied to clipboard

JsonbDateFormat deserialization fails

Open diegosilval opened this issue 7 years ago • 10 comments
trafficstars

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

diegosilval avatar Oct 11 '18 22:10 diegosilval

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?

bravehorsie avatar Oct 12 '18 09:10 bravehorsie

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;
    }

}

}

diegosilval avatar Oct 12 '18 14:10 diegosilval

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.

pdudits avatar Apr 12 '19 13:04 pdudits

Are there any news about this issue? I have the same problem reported by @pdudits

notarmara avatar May 09 '19 10:05 notarmara

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

diegosilval avatar May 09 '19 16:05 diegosilval

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.

notarmara avatar May 10 '19 08:05 notarmara

The issue still persists with 1.0.4 but works fine with LocalDate

manish2aug avatar Jul 15 '19 09:07 manish2aug

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.

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?

agbaroni avatar Mar 26 '20 16:03 agbaroni

I have the same issue when with zoned date-time, any updates on this yet?

chriseteka avatar Oct 18 '21 08:10 chriseteka

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...

black-pwq avatar Apr 19 '24 07:04 black-pwq