Models

Learn how to derive the JSON codecs in our data models using Circe.

We'll cover the following...

Recap

We will reuse the models that we have already written here.

To recap, these were our models:

Press + to interact
final case class Translation(lang: LanguageCode, name: ProductName)
object Translation {
implicit val decode: Decoder[Translation] =
Decoder.forProduct2("lang", "name")(Translation.apply)
implicit val encode: Encoder[Translation] =
Encoder.forProduct2("lang", "name")(t => (t.lang, t.name))
}
type ProductId = java.util.UUID
final case class Product(id: ProductId, names: NonEmptySet[Translation])
object Product {
implicit val decode: Decoder[Product] =
Decoder.forProduct2("id", "names")(Product.apply)
implicit val encode: Encoder[Product] =
Encoder.forProduct2("id", "names")(p => (p.id, p.names))
}

Derive JSON codecs

The only thing we will change is add a semi-automatic derivation of the JSON codecs. We just need to import the appropriate Circe package and call the derive functions.

Access this course and 1400+ top-rated courses and projects.