Skip to content

Commit

Permalink
YearMonth and MonthDay support added (#492)
Browse files Browse the repository at this point in the history
YearMonth and MonthDay support added

Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed May 26, 2021
1 parent 4abf798 commit 5437221
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -22,9 +22,11 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -143,6 +145,10 @@ private static Map<Class<?>, SerializerProviderWrapper> initSerializers() {
serializers.put(XMLGregorianCalendar.class,
new SerializerProviderWrapper(XMLGregorianCalendarTypeSerializer::new,
XMLGregorianCalendarTypeDeserializer::new));
serializers.put(YearMonth.class,
new SerializerProviderWrapper(YearMonthTypeSerializer::new, YearMonthTypeDeserializer::new));
serializers.put(MonthDay.class,
new SerializerProviderWrapper(MonthDayTypeSerializer::new, MonthDayTypeDeserializer::new));

return serializers;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal.serializer;

import java.time.Instant;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import org.eclipse.yasson.internal.model.customization.Customization;

/**
* Deserializer for {@link MonthDay} type.
*/
public class MonthDayTypeDeserializer extends AbstractDateTimeDeserializer<MonthDay> {

private static final DateTimeFormatter DEFAULT_FORMAT = DateTimeFormatter.ofPattern("--MM-dd").withZone(UTC);

/**
* Creates an instance.
*
* @param customization Model customization.
*/
public MonthDayTypeDeserializer(Customization customization) {
super(MonthDay.class, customization);
}

@Override
protected MonthDay fromInstant(Instant instant) {
return MonthDay.from(instant.atZone(UTC));
}

@Override
protected MonthDay parseDefault(String jsonValue, Locale locale) {
return MonthDay.parse(jsonValue, DEFAULT_FORMAT.withLocale(locale));
}

@Override
protected MonthDay parseWithFormatter(String jsonValue, DateTimeFormatter formatter) {
return MonthDay.parse(jsonValue, formatter);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal.serializer;

import java.time.Instant;
import java.time.MonthDay;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import org.eclipse.yasson.internal.model.customization.Customization;

/**
* Serializer for {@link MonthDay} type.
*/
public class MonthDayTypeSerializer extends AbstractDateTimeSerializer<MonthDay> {

private static final int YEAR_NUMBER = Year.now().getValue();

private static final DateTimeFormatter DEFAULT_FORMAT = DateTimeFormatter.ofPattern("--MM-dd").withZone(UTC);

/**
* Creates a new instance.
*
* @param customization Model customization.
*/
public MonthDayTypeSerializer(Customization customization) {
super(customization);
}

@Override
protected Instant toInstant(MonthDay value) {
return value.atYear(YEAR_NUMBER).atStartOfDay(UTC).toInstant();
}

@Override
protected String formatDefault(MonthDay value, Locale locale) {
return DEFAULT_FORMAT.withLocale(locale).format(value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal.serializer;

import java.time.Instant;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import org.eclipse.yasson.internal.model.customization.Customization;

/**
* Deserializer for {@link YearMonth} type.
*/
public class YearMonthTypeDeserializer extends AbstractDateTimeDeserializer<YearMonth> {

private static final DateTimeFormatter DEFAULT_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM").withZone(UTC);

/**
* Creates an instance.
*
* @param customization Model customization.
*/
public YearMonthTypeDeserializer(Customization customization) {
super(YearMonth.class, customization);
}

@Override
protected YearMonth fromInstant(Instant instant) {
return YearMonth.from(instant.atZone(UTC));
}

@Override
protected YearMonth parseDefault(String jsonValue, Locale locale) {
return YearMonth.parse(jsonValue, DEFAULT_FORMAT.withLocale(locale));
}

@Override
protected YearMonth parseWithFormatter(String jsonValue, DateTimeFormatter formatter) {
return YearMonth.parse(jsonValue, formatter);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal.serializer;

import java.time.Instant;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import org.eclipse.yasson.internal.model.customization.Customization;

/**
* Serializer for {@link YearMonth} type.
*/
public class YearMonthTypeSerializer extends AbstractDateTimeSerializer<YearMonth> {

private static final DateTimeFormatter DEFAULT_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM").withZone(UTC);

/**
* Creates a new instance.
*
* @param customization Model customization.
*/
public YearMonthTypeSerializer(Customization customization) {
super(customization);
}

@Override
protected Instant toInstant(YearMonth value) {
return value.atDay(1).atStartOfDay(UTC).toInstant();
}

@Override
protected String formatDefault(YearMonth value, Locale locale) {
return DEFAULT_FORMAT.withLocale(locale).format(value);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -12,6 +12,8 @@

package org.eclipse.yasson.defaultmapping.dates;

import org.eclipse.yasson.defaultmapping.dates.model.MonthDayPojo;
import org.eclipse.yasson.defaultmapping.dates.model.YearMonthPojo;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.eclipse.yasson.Jsonbs.*;
Expand Down Expand Up @@ -52,9 +54,11 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -612,6 +616,34 @@ public void testDateInMap() {
assertEquals(LocalDate.of(2017,1,1), result.dateMap.get("first"));
}

@Test
public void testYearMonth() {
YearMonthPojo yearMonthPojo = new YearMonthPojo();
yearMonthPojo.yearMonth = YearMonth.of(2019, Month.MAY);
yearMonthPojo.yearMonthWithFormatter = YearMonth.of(2019, Month.MAY);

String expected = "{\"yearMonth\":\"2019-05\",\"yearMonthWithFormatter\":\"05-2019\"}";
String serialized = bindingJsonb.toJson(yearMonthPojo);
assertEquals(expected, serialized);

YearMonthPojo deserialized = bindingJsonb.fromJson(expected, YearMonthPojo.class);
assertEquals(yearMonthPojo, deserialized);
}

@Test
public void testMonthDay() {
MonthDayPojo monthDay = new MonthDayPojo();
monthDay.monthDay = MonthDay.of(Month.MAY, 2);
monthDay.monthDayWithFormatter = MonthDay.of(Month.MAY, 2);

String expected = "{\"monthDay\":\"--05-02\",\"monthDayWithFormatter\":\"02-05\"}";
String serialized = bindingJsonb.toJson(monthDay);
assertEquals(expected, serialized);

MonthDayPojo deserialized = bindingJsonb.fromJson(expected, MonthDayPojo.class);
assertEquals(monthDay, deserialized);
}

@Test
public void testXMLGregorianCalendar() throws DatatypeConfigurationException {
final Calendar calendar = new Calendar.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.defaultmapping.dates.model;

import java.time.MonthDay;
import java.util.Objects;

import jakarta.json.bind.annotation.JsonbDateFormat;

/**
* Pojo object of the {@link MonthDay}.
*/
public class MonthDayPojo {

public MonthDay monthDay;

@JsonbDateFormat("dd-MM")
public MonthDay monthDayWithFormatter;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MonthDayPojo that = (MonthDayPojo) o;
return Objects.equals(monthDay, that.monthDay) && Objects
.equals(monthDayWithFormatter, that.monthDayWithFormatter);
}

@Override
public int hashCode() {
return Objects.hash(monthDay, monthDayWithFormatter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.defaultmapping.dates.model;

import java.time.YearMonth;
import java.util.Objects;

import jakarta.json.bind.annotation.JsonbDateFormat;

/**
* Pojo object of the {@link YearMonth}.
*/
public class YearMonthPojo {

public YearMonth yearMonth;

@JsonbDateFormat("MM-yyyy")
public YearMonth yearMonthWithFormatter;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
YearMonthPojo that = (YearMonthPojo) o;
return Objects.equals(yearMonth, that.yearMonth) && Objects
.equals(yearMonthWithFormatter, that.yearMonthWithFormatter);
}

@Override
public int hashCode() {
return Objects.hash(yearMonth, yearMonthWithFormatter);
}
}

0 comments on commit 5437221

Please sign in to comment.