Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
henricook opened this issue May 1, 2020 · 3 comments

Comments

@henricook
Copy link

henricook commented May 1, 2020

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
Copy link
Author

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
Copy link

yianni commented May 4, 2020

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
Copy link

pjfanning commented May 30, 2020

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants