業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

play-json-extensions 0.42.0 の not found: value encoder エラー

Posted at

Play-Jsonで、case classのフィールド数が22を超えると Format オブジェクトの自動生成ができなくなる。そんなときに使えるやつ。

でも、

case class Bar(
  s: String,
  i: Int
)

object Bar {
  implicit lazy val format: Format[Bar] = Jsonx.formatCaseClassUseDefaults[Bar]
}

このコードは Jsonx.formatCaseClassUseDefaults に必要な暗黙パラメータがないということでエラーでる。

[error] xxx/src/main/scala/sample/Bar.scala:13:75: could not find implicit value for parameter encoder: ai.x.play.json.NameEncoder
[error]   implicit lazy val format: Format[Bar] = Jsonx.formatCaseClassUseDefaults[Bar]

ちなみに、0.40.x系ではこのエラーはでない。というか NameEncoder というトレイト自体存在しない。

とりあえずBarのコンパニオンオブジェクトに NameEncoder 型implicit変数を定義してやると

object Bar {
  implicit val jsonxNameEncoder: NameEncoder = CamelToSnakeNameEncoder()
  implicit lazy val format: Format[Bar] = Jsonx.formatCaseClassUseDefaults[Bar]
}

暗黙パラメータがないエラーは消えるけど、謎エラーでる。

[error] xxx/src/main/scala/sample/Bar.scala:13:75: not found: value encoder
[error]   implicit lazy val format: Format[Bar] = Jsonx.formatCaseClassUseDefaults[Bar]

ここでないと言われている encoder というのは、マクロが生成しているコード内で参照されている変数で、どうも、NameEncoder 型変数は encoder という名前でなければいけないっぽい。ええ・・・、こんな一般的な名前の変数をスコープ内に・・・?

object Bar {
  implicit val encoder: NameEncoder = CamelToSnakeNameEncoder()
  implicit lazy val format: Format[Bar] = Jsonx.formatCaseClassUseDefaults[Bar]
}

いちおう、これでOK。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?