aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/json.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/xyz/driver/core/json.scala')
-rw-r--r--src/main/scala/xyz/driver/core/json.scala19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala
index 6e780ed..c14424d 100644
--- a/src/main/scala/xyz/driver/core/json.scala
+++ b/src/main/scala/xyz/driver/core/json.scala
@@ -14,6 +14,8 @@ import xyz.driver.core.auth.AuthCredentials
import xyz.driver.core.date.{Date, Month}
import xyz.driver.core.domain.{Email, PhoneNumber}
import xyz.driver.core.time.Time
+import eu.timepit.refined.refineV
+import eu.timepit.refined.api.{Refined, Validate}
object json {
import DefaultJsonProtocol._
@@ -213,6 +215,23 @@ object json {
}
}
+ /**
+ * Provides the JsonFormat for the Refined types provided by the Refined library.
+ *
+ * @see https://github.com/fthomas/refined
+ */
+ implicit def refinedJsonFormat[T, Predicate](implicit valueFormat: JsonFormat[T],
+ validate: Validate[T, Predicate]): JsonFormat[Refined[T, Predicate]] =
+ new JsonFormat[Refined[T, Predicate]] {
+ def write(x: T Refined Predicate): JsValue = valueFormat.write(x.value)
+ def read(value: JsValue): T Refined Predicate = {
+ refineV[Predicate](valueFormat.read(value))(validate) match {
+ case Right(refinedValue) => refinedValue
+ case Left(refinementError) => deserializationError(refinementError)
+ }
+ }
+ }
+
val jsValueToStringMarshaller: Marshaller[JsValue, String] =
Marshaller.strict[JsValue, String](value => Marshalling.Opaque[String](() => value.compactPrint))