aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/json.scala
diff options
context:
space:
mode:
authorAleksandr <ognelisar@gmail.com>2018-04-03 10:48:10 +0700
committerAleksandr <ognelisar@gmail.com>2018-04-03 10:48:10 +0700
commitacf366c1a7f4b7dc7758d8a73b2e497068bb1fe8 (patch)
treefaf601fa831be596fffaeb737a52e508a1473523 /src/main/scala/xyz/driver/core/json.scala
parent04a21e9a5ab46f885cb51626d274d570fefe4a29 (diff)
parent322bbc9010e20195e5b0bb58e703961738ffb89d (diff)
downloaddriver-core-acf366c1a7f4b7dc7758d8a73b2e497068bb1fe8.tar.gz
driver-core-acf366c1a7f4b7dc7758d8a73b2e497068bb1fe8.tar.bz2
driver-core-acf366c1a7f4b7dc7758d8a73b2e497068bb1fe8.zip
Merge branch 'master' into TM-1431
Diffstat (limited to 'src/main/scala/xyz/driver/core/json.scala')
-rw-r--r--src/main/scala/xyz/driver/core/json.scala31
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): _*)