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.scala12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala
index 06a8837..e7efce6 100644
--- a/src/main/scala/xyz/driver/core/json.scala
+++ b/src/main/scala/xyz/driver/core/json.scala
@@ -34,16 +34,24 @@ object json {
}
}
- implicit def idFormat[T] = new RootJsonFormat[Id[T]] {
+ implicit def idFormat[T]: JsonFormat[Id[T]] = new JsonFormat[Id[T]] {
def write(id: Id[T]) = JsString(id.value)
- def read(value: JsValue) = value match {
+ def read(value: JsValue): Id[T] = value match {
case JsString(id) if Try(UUID.fromString(id)).isSuccess => Id[T](id.toLowerCase)
case JsString(id) => Id[T](id)
case _ => throw DeserializationException("Id expects string")
}
}
+ implicit def taggedFormat[F, T](implicit underlying: JsonFormat[F]): JsonFormat[F @@ T] = new JsonFormat[F @@ T] {
+ import tagging._
+
+ override def write(obj: F @@ T): JsValue = underlying.write(obj)
+
+ override def read(json: JsValue): F @@ T = underlying.read(json).tagged[T]
+ }
+
def NameInPath[T]: PathMatcher1[Name[T]] = new PathMatcher1[Name[T]] {
def apply(path: Path) = path match {
case Path.Segment(segment, tail) => Matched(tail, Tuple1(Name[T](segment)))