http4s / http4s-play-json   0.23.15

Apache License 2.0 GitHub

Play JSON codecs for http4s

Scala versions: 3.x 2.13 2.12

http4s-play-json CI Maven Central Cats friendly

Provides EntityEncoder and EntityDecoder support for play-json JSON library.

SBT Setup

libraryDependencies += "org.http4s" %% "http4s-play-json" % "0.23.13"

Usage

import org.http4s.play._
import play.api.libs.json._

// Lets fetch some JSON from a website and parse it 
// into a case class that has a play-json `Format` defined (Reads/Writes)

// Assumes that https://mydomain.invalid/sample.json responds:
// { "hello": "world" }

case class SampleResponse(hello: String)

object SampleResponse {
  // Play-json formatter for { "hello": "world" }
  implicit val format: Format[SampleResponse] = Json.format
}

// Fetch with an http4s client
val client: Client[F] = ???

val target = Uri.uri("http://mydomain.invalid/sample.json")

// The response will be deserialised to a `SampleResponse` 
// using the implicit play-json `Format`
val response: F[Sample] = httpClient.expect[SampleResponse](target)

Community

The Typelevel Discord has an #http4s channel. Please join us!

See also

Other JSON compatibility modules for http4s include: http4s-circe (in the core repo) and http4s-fabric.