play-json-extensions icon indicating copy to clipboard operation
play-json-extensions copied to clipboard

Could not find implicit value for parameter encoder (NameEncoder) with 0.42.0

Open henricook opened this issue 5 years ago • 3 comments
trafficstars

Hi all,

When upgrading to 0.42.0...

With a straightforward case class of only Boolean fields

[error] /home/me/repos/backend/app/com/foo/MyDto.scala:76:87: could not find implicit value for parameter encoder: ai.x.play.json.NameEncoder
[error]   implicit val format: OFormat[MyDto] = Jsonx.formatCaseClass[MyDto]

It's not clear from the docs or open issues what to do about it, can anyone help? Is it a bug?

henricook avatar May 01 '20 14:05 henricook

Looking through the MRs I think this is what introduced the breaking change - https://github.com/xdotai/play-json-extensions/pull/74/files

Can this issue remain as a reminder the readme needs to be updated? I think I should be using 'BaseNameEncoder' to retain previous behaviour but i'm not sure? Is that right?

henricook avatar May 02 '20 06:05 henricook

Like @henricook , I had to explore the source to fix the problem. In case anyone else is stuck here's a solution:

import ai.x.play.json.{BaseNameEncoder, Jsonx, NameEncoder}
import play.api.libs.json.{Json, OFormat}

case class Foo(a1: String, a2: String, a3: String, a4: String, a5: String,
               a6: String, a7: String, a8: String, a9: String, a10: String,
               a11: String, a12: String, a13: String, a14: String, a15: String,
               a16: String, a17: String, a18: String, a19: String, a20: String,
               a21: String, a22: String, a23: String)

object Foo {
  implicit val e: NameEncoder = BaseNameEncoder.apply()
  implicit val f: OFormat[Foo] = Jsonx.formatCaseClass[Foo]

  def empty(): Foo = {
    this ("", "", "", "", "",
      "", "", "", "", "",
      "", "", "", "", "",
      "", "", "", "", "",
      "", "", "")
  }
}

object MainObject {
  def main(args: Array[String]): Unit = {
    println(Json.toJson(Foo.empty()).as[Map[String, String]])
  }
}

yianni avatar May 04 '20 15:05 yianni

Thanks for the pointers. I found a slightly tidier solution - adding this import.

import ai.x.play.json.Encoders.encoder

pjfanning avatar May 30 '20 10:05 pjfanning