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

Jsonx.formatCaseClassUseDefaults for generic case class #25

Closed catap closed 8 years ago

catap commented 8 years ago

Good day!

I try create a format for generic case class. I have a code:


case class Page[T](offset: Int, limit: Int, values: List[T] = List())
case class Document(document_id: Int = Int.MinValue, name: String)

object Document {

  implicit val documentFormat = Jsonx.formatCaseClassUseDefaults[Document]

  implicit val documentPageFormat = Jsonx.formatCaseClassUseDefaults[Page[Document]]

}

When I try to compile it I have an error:

could not find implicit value for parameter helper: play.api.libs.json.Reads[List[T]]
TRIGGERED BY: could not find implicit value for parameter helper: ai.x.play.json.OptionValidationDispatcher[List[T]]
TO SOLVE THIS
1. Make sure there is a Reads[List[T]] or Format[List[T]] in the implicit scope
2. In case of Reads[Option[...]] you need to either
   import ai.x.play.json.implicits.optionWithNull // suggested
   or
   import ai.x.play.json.implicits.optionNoError // buggy play-json 2.3 behavior
3. In case of Reads[... .type]
   import ai.x.play.json.SingletonEncoder.simpleName
   import ai.x.play.json.implicits.formatSingleton

How can I solve it?

cvogt commented 8 years ago

Neither play-json nor play-json extensions support case classes with type parameters at the moment. So Page[T] is not going to work unfortunately. A PR providing a specialized error message for this case or even adding support (if possible) would certainly be appreciated ;).

cvogt commented 8 years ago

closing this as wontfix for now

catap commented 8 years ago

Got it.

Thanks!

mandoolala commented 3 years ago

is it still not fixed in the recent release?

mandoolala commented 3 years ago

I opened this issue #89 which seems similar to this one