From eeb3da73a4281a35ae562c032587737725a2aa7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20H=C3=A4nninen?= Date: Mon, 18 Mar 2024 11:38:12 +0200 Subject: [PATCH] Add support for OffsetDateTime --- src/main/scala/fi/oph/scalaschema/SchemaFactory.scala | 3 ++- src/main/scala/fi/oph/scalaschema/Serializer.scala | 3 ++- .../scala/fi/oph/scalaschema/extraction/DateExtractor.scala | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/scala/fi/oph/scalaschema/SchemaFactory.scala b/src/main/scala/fi/oph/scalaschema/SchemaFactory.scala index 9195539..5610cbd 100644 --- a/src/main/scala/fi/oph/scalaschema/SchemaFactory.scala +++ b/src/main/scala/fi/oph/scalaschema/SchemaFactory.scala @@ -2,7 +2,7 @@ package fi.oph.scalaschema import java.lang.reflect.Constructor import java.sql.Timestamp -import java.time.{LocalDate, LocalDateTime, ZonedDateTime} +import java.time.{LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime} import java.util.Date import fi.oph.scalaschema.Annotations.findAnnotations @@ -112,6 +112,7 @@ case class SchemaFactory() { "java.time.LocalDate" -> DateSchema(dateType = classOf[LocalDate]), "java.time.LocalDateTime" -> DateSchema(dateType = classOf[LocalDateTime]), "java.time.ZonedDateTime" -> DateSchema(dateType = classOf[ZonedDateTime]), + "java.time.OffsetDateTime" -> DateSchema(dateType = classOf[OffsetDateTime]), "java.lang.String" -> StringSchema(), "scala.Boolean" -> BooleanSchema(), "scala.Int" -> NumberSchema(numberType = classOf[Int]), diff --git a/src/main/scala/fi/oph/scalaschema/Serializer.scala b/src/main/scala/fi/oph/scalaschema/Serializer.scala index 6648e29..31f78b4 100644 --- a/src/main/scala/fi/oph/scalaschema/Serializer.scala +++ b/src/main/scala/fi/oph/scalaschema/Serializer.scala @@ -1,7 +1,7 @@ package fi.oph.scalaschema import java.time.format.DateTimeFormatter.ISO_INSTANT -import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime} +import java.time.{LocalDate, LocalDateTime, ZoneId, ZonedDateTime, OffsetDateTime} import java.util.Date import fi.oph.scalaschema.SchemaPropertyProcessor.SchemaPropertyProcessor import fi.oph.scalaschema.extraction.SchemaNotFoundException @@ -97,6 +97,7 @@ object Serializer { case x: LocalDateTime => JString(x.toString) case x: Date => JString(ISO_INSTANT.format(ZonedDateTime.ofInstant(x.toInstant, ZoneId.of("UTC")))) case x: ZonedDateTime => JString(x.toString) + case x: OffsetDateTime => JString(x.toString) case _ => throw new RuntimeException("Not a date: " + x) } diff --git a/src/main/scala/fi/oph/scalaschema/extraction/DateExtractor.scala b/src/main/scala/fi/oph/scalaschema/extraction/DateExtractor.scala index 52ef949..b2194e6 100644 --- a/src/main/scala/fi/oph/scalaschema/extraction/DateExtractor.scala +++ b/src/main/scala/fi/oph/scalaschema/extraction/DateExtractor.scala @@ -2,7 +2,7 @@ package fi.oph.scalaschema.extraction import java.sql.Timestamp import java.time.format.DateTimeParseException -import java.time.{LocalDate, LocalDateTime, ZonedDateTime} +import java.time.{LocalDate, LocalDateTime, ZonedDateTime, OffsetDateTime} import java.util.Date import fi.oph.scalaschema.{DateSchema, ExtractionContext, JsonCursor, Metadata} @@ -34,6 +34,8 @@ object DateExtractor { Date.from(java.time.ZonedDateTime.parse(dateString).toInstant) } else if (dateType == classOf[DateTime]) { ISODateTimeFormat.dateTimeParser.withZoneUTC.parseDateTime(dateString) + } else if (dateType == classOf[OffsetDateTime]) { + OffsetDateTime.parse(dateString) } else { throw new UnsupportedOperationException("Unrecognized Date type: " + dateType.getName) }