From 0ca4befab8aceb8008ffa3c7afc472528232f717 Mon Sep 17 00:00:00 2001 From: Stewart Stewart Date: Tue, 31 Jan 2017 23:22:59 -0500 Subject: serialize date to ISO string rather than object --- src/main/scala/xyz/driver/core/json.scala | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/main') diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala index 039f650..9a7672e 100644 --- a/src/main/scala/xyz/driver/core/json.scala +++ b/src/main/scala/xyz/driver/core/json.scala @@ -7,14 +7,12 @@ import akka.http.scaladsl.unmarshalling.Unmarshaller import spray.json.{DeserializationException, JsNumber, _} import xyz.driver.core.revision.Revision import xyz.driver.core.time.Time -import xyz.driver.core.date.{Date, Month} +import xyz.driver.core.date.Date import scala.reflect.runtime.universe._ object json { - import DefaultJsonProtocol._ - def IdInPath[T]: PathMatcher1[Id[T]] = new PathMatcher1[Id[T]] { def apply(path: Path) = path match { case Path.Segment(segment, tail) => Matched(tail, Tuple1(Id[T](segment))) @@ -69,16 +67,19 @@ object json { } } - implicit val monthFormat = new RootJsonFormat[Month] { - def write(month: Month) = JsNumber(month) - def read(value: JsValue): Month = value match { - case JsNumber(month) if 0 <= month && month <= 11 => date.tagMonth(month.toInt) - case _ => throw DeserializationException("Expected a number from 0 to 11") + implicit val dateFormat = new JsonFormat[Date] { + def write(date: Date) = JsString(date.toString) + def read(value: JsValue): Date = value match { + case JsString(dateString) => + Date + .fromString(dateString) + .getOrElse( + throw new DeserializationException( + s"Misformated ISO 8601 Date. Expected YYYY-MM-DD, but got $dateString.")) + case _ => throw new DeserializationException(s"Date expects a string, but got $value.") } } - implicit val dateFormat = jsonFormat3(Date.apply) - def RevisionInPath[T]: PathMatcher1[Revision[T]] = PathMatcher("""[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}""".r) flatMap { string => Some(Revision[T](string)) -- cgit v1.2.3