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

2

9

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)
}
Autoerotic answered 30/9, 2015 at 5:10 Comment(1)
Hi, did you have any chance to do what you wanted?Proliferation
G
1

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.

Ginkgo answered 7/9, 2018 at 10:59 Comment(0)
S
0

Adding these imports solved the issue for me. In your case just importing the first one should solve the issue.

import play.api.libs.functional.syntax._

import play.api.libs.json.{Json, Reads, _}
Songful answered 6/9, 2018 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.