-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
YearMonth and MonthDay support added (#492)
YearMonth and MonthDay support added Signed-off-by: David Kral <david.k.kral@oracle.com>
- Loading branch information
Showing
8 changed files
with
339 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/org/eclipse/yasson/internal/serializer/MonthDayTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/eclipse/yasson/internal/serializer/MonthDayTypeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/org/eclipse/yasson/internal/serializer/YearMonthTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/eclipse/yasson/internal/serializer/YearMonthTypeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/java/org/eclipse/yasson/defaultmapping/dates/model/MonthDayPojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/eclipse/yasson/defaultmapping/dates/model/YearMonthPojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |