Adds support for zio-json encoding/decoding to play.
Add the dependency to your build.sbt:
libraryDependencies += "com.schuwalow" %% "play-ziojson" % "0.1.0"
Define your model:
package models
import zio.json.{DeriveJsonCodec, JsonCodec}
final case class Foo(foo: Int)
object Foo {
implicit val codec: JsonCodec[Foo] = DeriveJsonCodec.gen[Foo]
}
Then encode and decode it in your controller!
package controllers
import com.schuwalow.play.ziojson.ZioJsonSupport
import models.Foo
import play.api.mvc._
import javax.inject._
import scala.concurrent.ExecutionContext
@Singleton
class FooController @Inject() (val controllerComponents: ControllerComponents)(implicit val ec: ExecutionContext)
extends BaseController
with ZioJsonSupport {
def getFoo() = Action {
Ok(ziojson.asJson(Foo(123)))
}
def postFoo() = Action(ziojson.json[Foo]) { req =>
Ok(s"Got a Foo: ${req.body.foo}")
}
}