Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ISO_DATE to Date deserialization #648

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -13,11 +14,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 +41,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
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation. All rights reserved.
* Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -82,7 +83,7 @@
* @author Dmitry Kornilov
*/
public class DatesTest {

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

@SuppressWarnings("serial")
Expand All @@ -98,7 +99,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 +127,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 +148,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 +171,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 +229,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