Can anyone show me how to convert a POJO or class instance to JSON in the Play platform (especially Play v2.3.x) using Scala?
For example, I have a code like this:
case class Foo(name: String, address: String) def index = Action { request => { val foo = Foo("John Derp", "Jem Street 21")
Error message:
Unable to write an instance of com.fasterxml.jackson.data bind.JsonNode for an HTTP response. Try defining Writeable [com.fasterxml.jackson.databind.JsonNode]
UPDATE: I found that the above error was caused by improper import of the Json class, it should be: import play.api.libs.json.Json . However, I still got the error with the implicit issue below.
I read this tutorial , but when I tried the implicit Writes[Foo] code:
implicit val fooWrites: Writes[Foo] = ( (JsPath \ "name").write[String] and (JsPath \ "address").write[String] )(unlift(Foo.unapply))
I got the error Can't resolve symbol and and Can't resolve symbol unlift in Intellij. Also, the tutorial code looks complicated only for converting an object to JSON. I wonder if there is an easier way to do this?
source share