Skip to content

Instantly share code, notes, and snippets.

@diamondo25
Created November 23, 2016 14:15
Show Gist options
  • Save diamondo25/bedd20a997fb0d60c6f29d27def5e766 to your computer and use it in GitHub Desktop.
Save diamondo25/bedd20a997fb0d60c6f29d27def5e766 to your computer and use it in GitHub Desktop.
Pretty print Json in result in Play Framework 2.5
package controllers
import javax.inject.{Inject, Singleton}
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}
@Singleton
class HomeController @Inject() () extends Controller with PrettyJsonOutput {
def index = Action { implicit request =>
Ok(Json.obj(
"greeting" -> "Hello"
))
}
}
package controllers
import play.api.http.Writeable
import play.api.libs.json.{JsValue, Json}
import play.api.mvc.Codec
trait PrettyJsonOutput {
implicit def writeableOf_PrettyJsValue(implicit codec: Codec): Writeable[JsValue] = {
Writeable(a => codec.encode(Json.prettyPrint(a)))
}
}
{
"greeting" : "Hello"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment