bizzabo / play-json-extensions

+22 field case class formatter and more for play-json
http://cvogt.org/play-json-extensions/api/
Other
196 stars 44 forks source link

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

Open henricook opened 4 years ago

henricook commented 4 years ago

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 commented 4 years ago

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?

yianni commented 4 years ago

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]])
  }
}
pjfanning commented 4 years ago

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

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