Custom Json Writes with combinators - not all the fields of the case class are needed
json, playframework-2.0, scala
Solution
If you are using Playframework 2.2 (not sure about earlier versions, but it should work as well) try this:
implicit val writer = new Writes[Foo] {
def writes(foo: Foo): JsValue = {
Json.obj("a" -> foo.a,
"b" -> foo.b)
}
}
Problem
I'm trying to write a custom Json serializer in play for a case class but I don't want it to serialize all the fields of the class. I'm pretty new to Scala, so that is surely the problem but this is what I tried so far: ``` case class Foo(a: String, b: Int, c: Double) ``` Now the default way of doing this, as far as I saw in the examples is: ``` implicit val fooWrites: Writes[Foo] = ( (__ \ "a").write[String] and (__ \ "b").write[Int] (__ \ "c").write[Double] ) (unlift(Foo.unapply)) ``` But what if I want to omit "c" from the Json output? I've tried this so far but it doesn't compile: ``` implicit val fooWritesAlt: Writes[Foo] = ( (__ \ "a").write[String] and (__ \ "b").write[Int] ) (unlift({(f: Foo) => Some((f.a, f.b))})) ``` Any help is greatly appreciated!