Working with JSON

Json values are supported through codecs which encode/decode values to json strings. However, third-party libraries are needed for actual json parsing/printing. Currently, Circe is supported. To use, add the following dependency to your project:

"com.softwaremill.tapir" %% "tapir-json-circe" % "0.3"

Next, import the package (or extend the JsonCirce trait, see MyTapir):

import tapir.json.circe._

This will bring into scope Codecs which, given an in-scope circe Encoder/Decoder, will create a codec using the json media type. Circe includes a couple of approaches to generating encoders/decoders (manual, semi-auto and auto), so you may choose whatever suits you.

For example, to automatically generate a JSON codec for a case class:

import tapir._
import tapir.json.circe._
import io.circe.generic.auto._

case class Book(author: String, title: String, year: Int)

val bookInput: EndpointIO[Book] = jsonBody[Book]

To add support for other JSON libraries, see the sources for the Circe codec (which is just a couple of lines of code).