1
votes

If I have a trait such as:

trait MyTrait[T] {
    def myVal: T
}

And then I have a case class such as:

case class MyClass(val foo: Int, val myVal: Boolean) extends MyTrait[Boolean]

Finally, I have another case class

case class MyOtherClass(val bar: MyTrait[_])

How can I serialize and deserialize this last class to Json?

I made the reads and writes for myClass as follows:

object MyClass {
    implicit def MyClassWrites(implicit fmt: MyClass): Writes[MyClass] = new Writes[MyClass] {
        override def writes(m: MyClass) = Json.obj(
            "foo" -> m.foo,
            "myVal" -> m.myVal // myVal should be Boolean
        )
     }

    implicit def MyClassReads(implicit fmt: Reads[MyClass]): Reads[MyClass] = new Reads[MyClass] {
        override def reads(json: JsValue): MyClass = new MyClass(
            (json \ "foo").as[Int],
            true
        )
    }
}

myOtherClass also has a reads and writes, as follows:

object MyOtherClass {
    implicit def MyOtherClassWrites(implicit fmt: MyOtherClass): Writes[MyOtherClass] = new Writes[MyOtherClass] {
        override def writes(m: MyOtherClass) = Json.obj(
            "bar" -> Json.toJson(m.bar)
        )
    }
    implicit def MyOtherClassReads(implicit fmt: Reads[MyOtherClass]): Reads[MyOtherClass] = new Reads[MyOtherClass] {
        override def reads(json: JsValue): MyClass = new MyOtherClass(
            (json \ "bar").as[MyClass[Boolean]]
        )
    }
}

When compiling, I get the following error:

 Error:(43, 28) No Json serializer found for type A$A135.this.MyTrait[_$1]. Try to implement an implicit Writes or Format for this type.
    "bar" -> Json.toJson(m.bar)
             ^

If I add a Writes to the trait as following:

object MyTrait {
    implicit def MyClassWrites(implicit fmt: MyTrait[_]): Writes[MyTrait[_]] = new Writes[MyTrait[_]] {
      override def writes(m: MyTrait[_]) = Json.obj(
          "myVal" -> Json.toJson(m.myVal) // myVal should be Boolean
      )
    }

    implicit def MyClassReads(implicit fmt: Reads[MyTrait[_]]): Reads[MyTrait[_]] = new Reads[MyTrait[_]] {
        override def reads(json: JsValue): MyTrait[_] = new MyClass(2, false)
    }
}

I now get this error:

Error:(10, 30) No Json serializer found for type _$4. Try to implement an implicit Writes or Format for this type.
    "myVal" -> Json.toJson(m.myVal) // myVal should be Boolean
               ^

I'm lost here, isn't there a way to serialize bar as if it is a MyClass?

I'vr also tried to change the writer for MyTrait as following:

override def writes(m: MyTrait[_]) = {
    m match {
        case c: MyClass => Json.toJson(c)
    }
}

Now the writer for the trait compiles but we get back to the first error:

Error:(45, 28) No Json serializer found for type A$A199.this.MyTrait[_$9]. Try to implement an implicit Writes or Format for this type.
    "bar" -> Json.toJson(m.bar)
             ^

I don't get it, I have defined the writers and readers, haven't I?.

Thanks in advance.

1

1 Answers

0
votes

Try to defines the following instances.

implicit def MyTRead[T](implicit r: Reads[T]): Reads[MyTrait[T]] = ???
implicit def MyTWrite[T](implicit r: Writes[T]): Writes[MyTrait[T]] = ???