Hi Guys,
I have a working controller which is happily serializing case classes to JSON. The problem is that I have some non-case class models that I need to render as well.
I'm attempting to follow the Json4S guide here:
https://github.com/json4s/json4s (specifically the serializing non-supported types), but all I get is a toString() of my model object.
Here is a snippet of my controller:
class MainServlet extends ScalatraServlet with JacksonJsonSupport {
implicit override val jsonFormats: Formats = DefaultFormats + new TestSerializer
get("/foo") {
Ok(new Test(123))
}
}
class Test(val i: Int)
class TestSerializer extends CustomSerializer[Test](format => (
{
case JObject(JField("i", JInt(i)) :: Nil) =>
new Test(i.toInt)
},
{
case x: Test =>
JObject(JField("i", JInt(x.i)) :: Nil)
}
))