play-json-derived-codecs icon indicating copy to clipboard operation
play-json-derived-codecs copied to clipboard

How to use DerivedReads with 2 params?

Open edrevo opened this issue 4 years ago • 1 comments

I am trying to migrate from 5.0.0 to 7.0.0, but I see that DerivedReads now has an extra TypeTag parameter. The README contains the following snippet, which I'm not sure works anymore with 7.0.0:

implicit val SecondReads: DerivedReads[Second] = new DerivedReads[Second] {
  def reads(tagReads: TypeTagReads, adapter: NameAdapter) = tagReads.reads("Second", (__ \ "foo").read[Integer].map(foo => Second(foo)))
}

since the compiler in my case is complaining that DerivedReads needs 2 params. Is there any ergonomic way of "ignoring" the second type param? My current code looks like this:

implicit def reads(implicit derivedReads: Lazy[DerivedReads[ApiEventV1]]): Reads[ApiEventV1] =
    derivedReads.value.reads(customTypeTagRead, NameAdapter.snakeCase)

And I would like to basically accept whatever in the second type param, but _ isn't working since the second type param is a generic type.

edrevo avatar Aug 11 '20 06:08 edrevo

Indeed, the documentation is outdated, we must update it.

Maybe the following works?

implicit def reads[X](implicit derivedReads: Lazy[DerivedReads[ApiEventV1, X]]): Reads[ApiEventV1] =
    derivedReads.value.reads(customTypeTagRead, NameAdapter.snakeCase)

However, maybe in your case you can use derived.withTypeTag? See API documentation here

julienrf avatar Aug 12 '20 14:08 julienrf