I have following classes
import spray.json._
sealed trait Base
case class Foo[K, V](key : K, value : V) extends Base
case class Bar[K, V](key : V, value : K) extends Base
their corresponding json convertors
implicit def baseJsonConvertor[K: JsonFormat, V: JsonFormat] = new JsonFormat[Base] {
override def read(json: JsValue): Base =
throw new SerializationException("Don't use this for reading...")
override def write(obj: Base): JsValue = obj match {
case e : Foo[K, V] => jsonFormat2(Foo.apply[K, V]).toJson
case e : Bar[K, V] => jsonFormat2(Bar.apply[V, K]).toJson
}
}
implicit def fooJsonConvertor[K: JsonFormat, V: JsonFormat] = jsonFormat2(Foo.apply[K, V])
implicit def barJsonConvertor[K: JsonFormat, V: JsonFormat] = jsonFormat2(Bar.apply[V, K])
When I try this
val list = List(Foo[String, Int]("One", 1), Bar(2, "Two") ).map(_.toJson)
I get this error as
:216: warning: abstract type K in type pattern Foo[K,V] is unchecked since it is eliminated by erasure case e : Foo[K, V] => jsonFormat2(Foo.apply[K, V]).toJson
Cannot find JsonWriter or JsonFormat type class for Product with Serializable with Base val list = List(FooString, Int ).map(_.toJson)
I can't use context bound in match-case
in write
method of baseJsonConvertor
.
Can someone help with the work around for this problem?