I need to receive a big JSON on my server (more than 22 fields). I have a case class with a lot of fields:
case class Filters(objectType: Option[String] = None,
deal: Option[String] = None,
roomsCount: Option[String] = None,
region: Option[Int] = None,
district: Option[Int] = None,
direction: Option[Int] = None
...
)
And JSON format function in controller:
implicit val filtersFormat = Json.format[Filters]
At compilation I have error:
[error] WebSockets.scala:18: No unapply or unapplySeq function found
[error] implicit val filtersFormat = Json.format[Filters]
[error] ^
Is there a way to solve the problem without breaking JSON to small parts?
I did it this way:
case class Filters(part1: Part1, part2: Part2, ...)
case class Part1(
field1: Field1,
field2: Field2,
...
field10: Field10,
)
object Part1 {
implicit val part1Format = Json.format[Part1]
}
...
object Filters {
implicit val filtersReads = (
JsPath.read[Part1] and
JsPath.read[Part2] and
...
)(Filters.apply _)
implicit val filtersWrites = (
JsPath.write[Part1] and
JsPath.write[Part2] and
...
)(unlift(Filters.unapply))
}
As @Sawyer mentioned you can use an external library. This was taken from this blog.
First add the lib dependency to your SBT
libraryDependencies += "ai.x" %% "play-json-extensions" % "0.42.0"
Then you can use it like this:
import ai.x.play.json.Jsonx
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 f: OFormat[Foo] = Jsonx.formatCaseClass[Foo]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With