diff options
author | Arthur Rand <arand@ucsc.edu> | 2018-03-28 05:56:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-28 05:56:21 -0700 |
commit | fc6ecfe212c84271a3454617054aaf25890e886a (patch) | |
tree | 4d6b85e059e10aa5b461af9e21c32c84bd73250f /src/main/scala/xyz/driver/core/json.scala | |
parent | 30dba9ebf2abfd06452f46bac2b4c922043f56e6 (diff) | |
download | driver-core-fc6ecfe212c84271a3454617054aaf25890e886a.tar.gz driver-core-fc6ecfe212c84271a3454617054aaf25890e886a.tar.bz2 driver-core-fc6ecfe212c84271a3454617054aaf25890e886a.zip |
[API-1468] add TimeOfDay (#141)v1.8.11
* add TimeOfDay
* add formatter
* .
* Revert "."
This reverts commit 89576de98092dd75d3af7d82d244d5eaa24d31d9.
* scalafmt
* add before and after to ToD, and tests
* rearrage, make fromStrings
* add generator
* address comments
* use explicit string for TimeZoneId
* renaming
* revert Converters changes
* change name of private method
* change apply method
* use month
Diffstat (limited to 'src/main/scala/xyz/driver/core/json.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/json.scala | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala index 02a35fd..4d7fa04 100644 --- a/src/main/scala/xyz/driver/core/json.scala +++ b/src/main/scala/xyz/driver/core/json.scala @@ -1,7 +1,7 @@ package xyz.driver.core import java.net.InetAddress -import java.util.UUID +import java.util.{TimeZone, UUID} import scala.reflect.runtime.universe._ import scala.util.Try @@ -14,7 +14,7 @@ import spray.json._ import xyz.driver.core.auth.AuthCredentials import xyz.driver.core.date.{Date, DayOfWeek, Month} import xyz.driver.core.domain.{Email, PhoneNumber} -import xyz.driver.core.time.Time +import xyz.driver.core.time.{Time, TimeOfDay} import eu.timepit.refined.refineV import eu.timepit.refined.api.{Refined, Validate} import eu.timepit.refined.collection.NonEmpty @@ -80,6 +80,33 @@ object json { } } + implicit object localTimeFormat extends JsonFormat[java.time.LocalTime] { + private val formatter = TimeOfDay.getFormatter + def read(json: JsValue): java.time.LocalTime = json match { + case JsString(chars) => + java.time.LocalTime.parse(chars) + case _ => deserializationError(s"Expected time string got ${json.toString}") + } + + def write(obj: java.time.LocalTime): JsValue = { + JsString(obj.format(formatter)) + } + } + + implicit object timeZoneFormat extends JsonFormat[java.util.TimeZone] { + override def write(obj: TimeZone): JsValue = { + JsString(obj.getID()) + } + + override def read(json: JsValue): TimeZone = json match { + case JsString(chars) => + java.util.TimeZone.getTimeZone(chars) + case _ => deserializationError(s"Expected time zone string got ${json.toString}") + } + } + + implicit val timeOfDayFormat: RootJsonFormat[TimeOfDay] = jsonFormat2(TimeOfDay.apply) + implicit val dayOfWeekFormat: JsonFormat[DayOfWeek] = new EnumJsonFormat[DayOfWeek](DayOfWeek.All.map(w => w.toString -> w)(collection.breakOut): _*) |