aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/json.scala
diff options
context:
space:
mode:
authorArthur Rand <arand@ucsc.edu>2018-05-16 05:55:27 -0700
committerGitHub <noreply@github.com>2018-05-16 05:55:27 -0700
commit57cd7aaf5a2154af698fddc39f4bff8fd3e4f6e7 (patch)
treeefc31d48b45c10d2d2b911ab1476f2c13e86c98c /src/main/scala/xyz/driver/core/json.scala
parent803c76244bd7a4772f727e5b47f84bcc9f5adcd4 (diff)
downloaddriver-core-57cd7aaf5a2154af698fddc39f4bff8fd3e4f6e7.tar.gz
driver-core-57cd7aaf5a2154af698fddc39f4bff8fd3e4f6e7.tar.bz2
driver-core-57cd7aaf5a2154af698fddc39f4bff8fd3e4f6e7.zip
[API-1584] Change AuthCredentials to accept a string identifier (#166)v1.9.3
* make email optional, add optional phone number to AuthCredentials * make AuthCredentials take a String instead of an email * wrap phone number parsing in Try * add json formatter for AuthCredentials * try val
Diffstat (limited to 'src/main/scala/xyz/driver/core/json.scala')
-rw-r--r--src/main/scala/xyz/driver/core/json.scala28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala
index aeb3453..c1c5862 100644
--- a/src/main/scala/xyz/driver/core/json.scala
+++ b/src/main/scala/xyz/driver/core/json.scala
@@ -187,7 +187,33 @@ object json {
implicit val phoneNumberFormat = jsonFormat2(PhoneNumber.apply)
- implicit val authCredentialsFormat = jsonFormat2(AuthCredentials)
+ implicit val authCredentialsFormat = new RootJsonFormat[AuthCredentials] {
+ override def read(json: JsValue): AuthCredentials = {
+ json match {
+ case JsObject(fields) =>
+ val emailField = fields.get("email")
+ val identifierField = fields.get("identifier")
+ val passwordField = fields.get("password")
+
+ (emailField, identifierField, passwordField) match {
+ case (_, _, None) =>
+ deserializationError("password field must be set")
+ case (Some(JsString(em)), _, Some(JsString(pw))) =>
+ val email = Email.parse(em).getOrElse(throw deserializationError(s"failed to parse email $em"))
+ AuthCredentials(email.toString, pw)
+ case (_, Some(JsString(id)), Some(JsString(pw))) => AuthCredentials(id.toString, pw.toString)
+ case (None, None, _) => deserializationError("identifier must be provided")
+ case _ => deserializationError(s"failed to deserialize ${json.prettyPrint}")
+ }
+ case _ => deserializationError(s"failed to deserialize ${json.prettyPrint}")
+ }
+ }
+
+ override def write(obj: AuthCredentials): JsValue = JsObject(
+ "identifier" -> JsString(obj.identifier),
+ "password" -> JsString(obj.password)
+ )
+ }
implicit object inetAddressFormat extends JsonFormat[InetAddress] {
override def read(json: JsValue): InetAddress = json match {