Search code examples
scalaplay-json

How to get the implicit reads from an object defined of some type?


I have an implicit defined for Reads[Seq[SomeCaseClass]] in an object:

implicit object myReadsObj extends Reads[Seq[SomeCaseClass]] {
    override def reads(js: JsValue): JsResult[Seq[SomeCaseClass]] = js match {
      // resulting JsResult[Seq[SomeCaseClass]]
    }
 }

How do I extract the Reads from it to pass as an implicit to other function asking for Reads, reads:Reads[Seq[A]] ?


Solution

  • I am not sure that this answers the question, but myReadsObj will be used as the Reads in the following code:

    case class SomeCaseClass()
    
    implicit object myReadsObj extends Reads[Seq[SomeCaseClass]] {
      override def reads(js: JsValue): JsResult[Seq[SomeCaseClass]] =
        JsSuccess(Seq(SomeCaseClass()))
    }
    
    val j = Json.parse("""{ "hello": "world" }""")
    j.validate[Seq[SomeCaseClass]]