A Modifier is metadata on a schema, or on one of its fields or cases, that codecs and other derivations read: it renames a field in JSON, accepts an old name, leaves a field out, or carries format-specific configuration. Modifiers are plain data with schemas of their own, so they can be serialized together with a schema.
Every modifier is also an annotation. Schema.derived reads Modifier.Term annotations on fields and cases, and Modifier.Reflect annotations on the type. To leave the type unannotated, add them with Schema#modifier for the type, or with DerivationBuilder#modifier or Deriver#withModifier for one derivation.
Which modifiers a codec reads depends on its format: the JSON codec reads all the built-in ones except id and config, while the Avro and MessagePack codecs, for example, ignore transient, rename and alias.
case class Book(
@Modifier.rename("name") title: String,
@Modifier.alias("published") year: Int,
@Modifier.transient() notes: String = ""
)
object Book {
implicit val schema: Schema[Book] = Schema.derived
}
val json = Book("Zionomicon", 2021, "signed copy").toJsonString
// {"name":"Zionomicon","year":2021}
val book = """{"name":"Zionomicon","published":2021}""".fromJson[Book]
// Right(Book(Zionomicon,2021,))
Attributes
- See also
- Companion
- object
- Source
- Modifier.scala
- Graph
-
- Supertypes
- Known subtypes
-
trait Reflectclass caseNamingclass configclass discriminatorclass fieldNamingclass noExtraFieldstrait Termclass aliasclass encodeTransientclass idclass renameclass transient