Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jilen/play-circe
circe for play
https://github.com/jilen/play-circe
circe json playframework
Last synced: 5 days ago
JSON representation
circe for play
- Host: GitHub
- URL: https://github.com/jilen/play-circe
- Owner: jilen
- License: apache-2.0
- Created: 2015-11-06T10:14:57.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-09-29T01:20:19.000Z (4 months ago)
- Last Synced: 2024-10-12T09:18:34.450Z (4 months ago)
- Topics: circe, json, playframework
- Language: Scala
- Size: 322 KB
- Stars: 108
- Watchers: 5
- Forks: 21
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[Circe](https://github.com/travisbrown/circe) support for [playframework](https://playframework.com/)
=====================================================================================================
[![Build](https://github.com/jilen/play-circe/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/jilen/play-circe/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/jilen/play-circe/branch/master/graph/badge.svg?token=SdVZrdCrzT)](https://codecov.io/gh/jilen/play-circe)How to get it
-------------- Add dependency
For play 2.6.x
> libraryDependencies += "com.dripower" %% "play-circe" % "2612.0"
For play 2.7.x
> libraryDependencies += "com.dripower" %% "play-circe" % "2712.0"
For play 2.8.x
> libraryDependencies += "com.dripower" %% "play-circe" % "2814.1"
For play 3.0.x
> libraryDependencies += "com.dripower" %% "play-circe" % "3014.1"
Usage
-----```scala
package play.api.libs.circeimport io.circe.generic.auto._
import io.circe.syntax._
import play.api._
import play.api.mvc._class CirceController(val controllerComponents: ControllerComponents) extends BaseController with Circe {
case class Bar(bar: Int)
case class Foo(foo: String, bar: Bar)val bar = Bar(1)
val foo = Foo("foo", bar)//serve json
def get = Action {
Ok(foo.asJson)
}//parse json to case class
def post = Action(circe.json[Foo]) { implicit request =>
val isEqual = request.body == foo
Ok(isEqual.toString)
}def postJson = Action(circe.json) { implicit request =>
val isEqual = request.body == foo.asJson
Ok(isEqual.toString)
}def postTolerate = Action(circe.tolerantJson[Foo]) { implicit request =>
val isEqual = request.body == foo
Ok(isEqual.toString)
}def postTolerateJson = Action(circe.tolerantJson) { implicit request =>
val isEqual = request.body == foo.asJson
Ok(isEqual.toString)
}
}
```# FAQ
+ If you want to customize the json output, you can provide an implicit `Printer` in scope
(default is `Printer.noSpaces`):```scala
import io.circe.Printerimplicit val customPrinter = Printer.spaces2.copy(dropNullValues = true)
```+ The `Circe` totally ignores the configured `HttpErrorHandler` and just uses `DefaultHttpErrorHandler`.
If this not what you want, simply make a trait to override `circeErrorHandler` like this
```scala
class MyController @Inject() (val errorHandler: HttpErrorHandler, val controllerComponents: ControllerComponents) extends BaseController with Circe {
override def circeErrorHandler = errorHandler
}
```