Рассмотрим сокращение нескольких ветвей JSON на и затем состав:
(__ \ "a1" ).json.prune andThen (__ \ "a2" ).json.prune
Вот рабочий пример
import ai.x.play.json.Jsonx
import play.api.libs.json.Json
import play.api.libs.json._
case class Foo(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int, a24: Int)
object Foo {
implicit val format = Jsonx.formatCaseClass[Foo]
}
object ExcludeFieldsFromSerialisation extends App {
val pruneTransformation = (field: String) => (__ \ field).json.prune
val excludedFields = List("a1", "a2").map(pruneTransformation).reduce((prune1, prune2) => prune1 andThen prune2)
val json = Json.toJson(Foo(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24))
val prunedJson = json.transform(excludedFields)
println(prunedJson.get)
}
который должен вывести
{
"a2": 2,
"a3": 3,
"a4": 4,
"a5": 5,
"a6": 6,
"a7": 7,
"a8": 8,
"a9": 9
"a10": 10,
"a11": 11,
"a12": 12,
"a13": 13,
"a14": 14,
"a15": 15,
"a16": 16,
"a17": 16,
"a18": 18,
"a19": 19,
"a20": 20,
"a21": 21,
"a22": 22,
"a23": 23,
"a24": 24,
}