diff options
author | vlad <vlad@driver.xyz> | 2017-02-02 19:01:00 -0500 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-02-02 19:01:00 -0500 |
commit | 191d34e55342cf2f1b8af0512f5358326693c780 (patch) | |
tree | 2dcde592ae37692c7e5dfbc049e3cda9c513d6a6 /src/main/scala/xyz/driver/core/json.scala | |
parent | e77031444ab667cbb8e7c3f114349a67e6451eac (diff) | |
download | driver-core-191d34e55342cf2f1b8af0512f5358326693c780.tar.gz driver-core-191d34e55342cf2f1b8af0512f5358326693c780.tar.bz2 driver-core-191d34e55342cf2f1b8af0512f5358326693c780.zip |
Moved email and phone number to corev0.10.1
Diffstat (limited to 'src/main/scala/xyz/driver/core/json.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/json.scala | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala index 664ef48..3c0d8d4 100644 --- a/src/main/scala/xyz/driver/core/json.scala +++ b/src/main/scala/xyz/driver/core/json.scala @@ -5,13 +5,16 @@ import akka.http.scaladsl.server.PathMatcher.{Matched, Unmatched} import akka.http.scaladsl.server.{PathMatcher, _} import akka.http.scaladsl.unmarshalling.Unmarshaller import spray.json.{DeserializationException, JsNumber, _} +import xyz.driver.core.auth.AuthCredentials import xyz.driver.core.revision.Revision import xyz.driver.core.time.Time import xyz.driver.core.date.Date +import xyz.driver.core.domain.{Email, PhoneNumber} 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 { @@ -74,9 +77,8 @@ object json { 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.") + throw DeserializationException(s"Misformated ISO 8601 Date. Expected YYYY-MM-DD, but got $dateString.")) + case _ => throw DeserializationException(s"Date expects a string, but got $value.") } } @@ -106,6 +108,24 @@ object json { } } + implicit val emailFormat = new RootJsonFormat[Email] { + def write(email: Email) = JsString(email.username + "@" + email.domain) + def read(json: JsValue): Email = json match { + + case JsString(value) => + Email.parse(value).getOrElse { + deserializationError("Expected '@' symbol in email string as Email, but got " + json) + } + + case _ => + deserializationError("Expected string as Email, but got " + json) + } + } + + implicit val phoneNumberFormat = jsonFormat2(PhoneNumber.apply) + + implicit val authCredentialsFormat = jsonFormat2(AuthCredentials) + class EnumJsonFormat[T](mapping: (String, T)*) extends RootJsonFormat[T] { private val map = mapping.toMap |