Created
November 23, 2016 14:15
-
-
Save diamondo25/bedd20a997fb0d60c6f29d27def5e766 to your computer and use it in GitHub Desktop.
Pretty print Json in result in Play Framework 2.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"greeting" : "Hello" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment