Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play json merge formats for case class with more than 22 fields

I am trying to split format to multiple tuples so it can handle more than 22 fields in the case class. However, I got an error "value and is not a member of play.api.libs.json.Format". How can I merge multiple formats for a case class?

val fields1to2: Format[(Int, String)] = (
  (__ \ "a").format[Int] and
  (__ \ "b").format[String]
).tupled

val fields3to4: Format[(Boolean, List[Int])] = (
  (__ \ "c").format[Boolean] and
  (__ \ "d").format[List[Int]]
).tupled

implicit val hugeCaseClassReads: Format[Huge] = (
  fields1to2 and fields3to4 // "value and is not a member of play.api.libs.json.Format"
) {
  case ((a, b), (c, d)) =>  
    Huge(a, b, c, d)
}
like image 900
angelokh Avatar asked Sep 30 '15 05:09

angelokh


1 Answers

If you are not limited to use only Play-JSON then try the Play-Json extensions library:

import ai.x.play.json.Jsonx
implicit val hugeCaseClassReads: Format[Huge] = Jsonx.formatCaseClass

But a more handy, safe, and efficient option would be using of jsoniter-scala - it has built in support of case classes with huge numbers of fields.

like image 159
Andriy Plokhotnyuk Avatar answered Sep 20 '22 04:09

Andriy Plokhotnyuk