Skip to content

Commit

Permalink
Fixes eclipse-ee4j#487. Support for ISO_DATE to Date deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
OndroMih committed Aug 21, 2024
1 parent 14e68b6 commit d76faa3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
package org.eclipse.yasson.internal.deserializer.types;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.Locale;

import static java.time.ZoneId.systemDefault;

/**
* Deserializer of the {@link Date} type.
*/
Expand All @@ -36,7 +40,12 @@ Date fromInstant(Instant instant) {

@Override
Date parseDefault(String jsonValue, Locale locale) {
return parseWithOrWithoutZone(jsonValue, DEFAULT_DATE_TIME_FORMATTER.withLocale(locale));
try {
return parseWithOrWithoutZone(jsonValue, DEFAULT_DATE_TIME_FORMATTER.withLocale(locale));
} catch (DateTimeParseException e3) {
LocalDate localDate = LocalDate.parse(jsonValue, DateTimeFormatter.ISO_DATE);
return Date.from(localDate.atStartOfDay(systemDefault()).toInstant());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
* @author Dmitry Kornilov
*/
public class DatesTest {

private static LocalDate localDate = LocalDate.of(2018, 1, 31);

@SuppressWarnings("serial")
Expand All @@ -98,7 +98,7 @@ public static class SqlDateObj implements Serializable {
public java.util.Date utilDate = java.sql.Date.valueOf("2018-01-31");

}

public static class SqlDateFormatted {
@JsonbDateFormat(value = "yyyy-MM-dd")
public java.sql.Date sqlDate;
Expand Down Expand Up @@ -126,14 +126,14 @@ public void testUnmarshallSqlDate() {
assertEquals("2018-01-31", result.sqlDate.toString());
assertEquals("2018-01-31", result.utilDate.toString());
}

@Test
public void testSqlDateTimeZonesFormatted() {
testSqlDateWithTZFormatted(TimeZone.getTimeZone(ZoneId.of("Europe/Sofia")));
testSqlDateWithTZFormatted(TimeZone.getTimeZone(ZoneId.of("US/Hawaii")));
testSqlDateWithTZFormatted(TimeZone.getTimeZone(ZoneId.of("Australia/Sydney")));
}

private void testSqlDateWithTZFormatted(TimeZone tz) {
final TimeZone originalTZ = TimeZone.getDefault();
TimeZone.setDefault(tz);
Expand All @@ -147,14 +147,14 @@ private void testSqlDateWithTZFormatted(TimeZone tz) {
TimeZone.setDefault(originalTZ);
}
}

@Test
public void testSqlDateTimeZonesMillis() {
testSqlDateTimeZonesMillis(TimeZone.getTimeZone(ZoneId.of("Europe/Sofia")), -99712800000L);
testSqlDateTimeZonesMillis(TimeZone.getTimeZone(ZoneId.of("US/Hawaii")), -99669600000L);
testSqlDateTimeZonesMillis(TimeZone.getTimeZone(ZoneId.of("Australia/Sydney")), -99741600000L);
}

private void testSqlDateTimeZonesMillis(TimeZone tz, long expectedMs) {
final TimeZone originalTZ = TimeZone.getDefault();
TimeZone.setDefault(tz);
Expand All @@ -170,14 +170,14 @@ private void testSqlDateTimeZonesMillis(TimeZone tz, long expectedMs) {
TimeZone.setDefault(originalTZ);
}
}

@Test
public void testSqlDateTimeZones() {
testSqlDateWithTZ(TimeZone.getTimeZone(ZoneId.of("Europe/Sofia")));
testSqlDateWithTZ(TimeZone.getTimeZone(ZoneId.of("US/Hawaii")));
testSqlDateWithTZ(TimeZone.getTimeZone(ZoneId.of("Australia/Sydney")));
}

private void testSqlDateWithTZ(TimeZone tz) {
final TimeZone originalTZ = TimeZone.getDefault();
TimeZone.setDefault(tz);
Expand Down Expand Up @@ -228,30 +228,59 @@ public void testDate() throws ParseException {
assertEquals(parsedDate, result.millisFormatted);
}

@Test
public void testDateIn_ISO_DATE_format() throws ParseException {
final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
final TimeZone defaultTimeZone = TimeZone.getDefault();
final TimeZone testTimeZone = TimeZone.getTimeZone(ZoneOffset.ofHours(1));

sdf.setTimeZone(testTimeZone);
final Date parsedDate = sdf.parse("04.03.2015");

// defaultFormatted in ISO_DATE format
final String jsonText = "{\"defaultFormatted\":\"2015-03-04\"," +
"\"millisFormatted\":" + parsedDate.getTime()+ "," +
"\"customDate\":\"00:00:00 | 04-03-2015\"}";

// marshal to ISO format, UTC, which is testTimeZone - 1
final String expected = "{\"defaultFormatted\":\"2015-03-03T23:00:00Z[UTC]\"," +
"\"millisFormatted\":" + parsedDate.getTime()+ "," +
"\"customDate\":\"00:00:00 | 04-03-2015\"}";

TimeZone.setDefault(testTimeZone);
try {
final DatePojo pojo = bindingJsonb.fromJson(jsonText, DatePojo.class);
assertEquals(parsedDate, pojo.defaultFormatted);
assertEquals(expected, bindingJsonb.toJson(pojo));
} finally {
TimeZone.setDefault(defaultTimeZone);
}
}

@Test
public void testDateWithZoneOffset() throws ParseException {
// Test for Yasson-172
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
final Date parsedDate = sdf.parse("2018-11-02T00:00:00+01:00");

String jsonDateWithZone = "{\"dateWithZone\":\"2018-11-02T00:00:00+01:00\"}";
final DateWithZonePojo result = bindingJsonb.fromJson(jsonDateWithZone, DateWithZonePojo.class);

assertEquals(parsedDate, result.dateWithZone);
}

@Test
public void testDateWithZoneId() throws ParseException {
// Test for Yasson-172
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
final Date parsedDate = sdf.parse("2018-11-02T00:00:00+01:00");

String jsonDateWithZone = "{\"dateWithZone\":\"2018-11-02T00:00:00+01:00[Europe/Berlin]\"}";
final DateWithZonePojo result = bindingJsonb.fromJson(jsonDateWithZone, DateWithZonePojo.class);

assertEquals(parsedDate, result.dateWithZone);
}

@Test
public void testCalendar() {
final Calendar timeCalendar = new Calendar.Builder()
Expand Down

0 comments on commit d76faa3

Please sign in to comment.