Skip to main content
Version: 2.x

Derivation

Derivation is how ZIO JSON writes a JsonEncoder, JsonDecoder or JsonCodec for your own case classes and sealed traits at compile time, so you don't write them by hand. This section is the reference for everything that controls it. The pages in it are:

Starting Derivationโ€‹

On Scala 2 and Scala 3, call gen on one of the three derivation objects, usually in the companion object so the compiler finds the instance without an import:

CallProduces
DeriveJsonEncoder.genJsonEncoder[A]
DeriveJsonDecoder.genJsonDecoder[A]
DeriveJsonCodec.genJsonCodec[A]
import zio.json._

final case class Person(name: String, age: Int)

object Person {
implicit val codec: JsonCodec[Person] = DeriveJsonCodec.gen[Person]
}

println(Person("Ada", 36).toJson)
// {"name":"Ada","age":36}

On Scala 3 you can write a derives clause instead. It calls JsonCodec.derived (or JsonEncoder.derived, JsonDecoder.derived):

final case class Person(name: String, age: Int) derives JsonCodec

Derivation needs an instance for the parts of the type. For a sealed trait, the subtypes that have no instance of their own are derived together with the trait on both Scala versions. For a field whose type has no instance, Scala 3 derives one, while Scala 2 stops with a compile error, so give that type its own codec first. Scala 2 and Scala 3 Differences lists every behaviour that differs between the two.

Changing What Derivation Producesโ€‹

There are three tools, and each works at a different scope:

ToolScopeExample
AnnotationsOne type or one field@jsonField("user_id"), @jsonDiscriminator("type")
JsonCodecConfigurationEvery type derived where the implicit or given is in scopeJsonCodecConfiguration(fieldNameMapping = SnakeCase)
@jsonDeriveWrites the derivation call for you (Scala 2 only)@jsonDerive case class Person(...)

A configuration in scope sets the default for every derived type, and an annotation on a type or field takes priority over it. Here the configuration turns every field name into snake_case, and @jsonField still decides the name of one field:

import zio.json._

implicit val config: JsonCodecConfiguration =
JsonCodecConfiguration(fieldNameMapping = SnakeCase)
// config: JsonCodecConfiguration = JsonCodecConfiguration(
// sumTypeHandling = WrapperWithClassNameField,
// fieldNameMapping = <function1>,
// allowExtraFields = true,
// sumTypeMapping = <function1>,
// explicitNulls = false,
// explicitEmptyCollections = ExplicitEmptyCollections(
// encoding = true,
// decoding = true
// ),
// enumValuesAsStrings = false
// )

final case class Account(accountId: Int, @jsonField("name") displayName: String)

object Account {
implicit val codec: JsonCodec[Account] = DeriveJsonCodec.gen[Account]
}

println(Account(1, "Ada").toJson)
// {"account_id":1,"name":"Ada"}

On Scala 3 the configuration is a given:

given JsonCodecConfiguration = JsonCodecConfiguration(fieldNameMapping = SnakeCase)

final case class Account(accountId: Int, displayName: String) derives JsonCodec

When no configuration is in scope, derivation uses JsonCodecConfiguration.default, so derives and gen never need one.

See Alsoโ€‹

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–