Skip to main content
Version: 2.x

Annotations

ZIO JSON reads annotations on case classes, fields and sealed traits when it derives a codec with DeriveJsonCodec.gen, DeriveJsonEncoder.gen or DeriveJsonDecoder.gen. All of them live in the zio.json package, so import zio.json._ brings them in. The table lists each annotation, what it can be placed on, and its arguments.

AnnotationApplies toArgumentWhat it doesHow-to
@jsonFieldFieldname: StringSets the JSON name of one field. Wins over @jsonMemberNames.Field names
@jsonAliasesFieldalias: String, aliases: String*Adds names the decoder also accepts for the field. No effect on encoding.Field names
@jsonMemberNamesCase classformat: JsonMemberFormatApplies a name format to every field, for encoding and decoding.Field names
@jsonHintSubtype of a sealed familyname: StringSets the type hint of the subtype. The default is the short class name.Sealed traits
@jsonHintNamesSealed trait or classformat: JsonMemberFormatApplies a name format to every type hint of the family.Sealed traits
@jsonDiscriminatorSealed trait or classname: StringWrites the type hint as a field inside the object instead of a wrapper key.Sealed traits
@jsonNoExtraFieldsCase classNoneMakes decoding fail when the JSON has a field the class does not have.Optional fields
@jsonExplicitNullCase class or fieldNoneWrites None as null instead of leaving the field out.Optional fields
@jsonExplicitEmptyCollectionsCase class or fieldencoding: Boolean = true, decoding: Boolean = trueSets whether empty collections are written and required. On a field, encoding only.Optional fields
@jsonExcludeFieldNoneLeaves the field out of the encoded JSON. The decoder still reads it.Optional fields
@jsonDeriveCase class or sealed trait (Scala 2 only)config: JsonDeriveConfig = JsonDeriveConfig.CodecGenerates the codec in the companion object at compile time.zio-json-macros

Most of these annotations have a program-wide form in JsonCodecConfiguration. The how-to page of each annotation names it.

Member Formatsโ€‹

@jsonMemberNames and @jsonHintNames take a JsonMemberFormat. The built-in formats are SnakeCase, KebabCase, PascalCase, CamelCase, IdentityFormat and CustomCase(f: String => String).

ziojson_03.SnakeCase and ziojson_03.KebabCase are the legacy formats of zio-json 0.3.0. They also put a separator before a digit, so abc123Def becomes abc_123_def. Use them only to keep JSON that was written by that version readable.

jsonDeriveโ€‹

@jsonDerive is different from the other annotations: it does not change how a codec behaves, it writes the codec for you. It is a Scala 2 macro annotation from the separate zio-json-macros module, and it needs -Ymacro-annotations on Scala 2.13 or the macro paradise plugin on Scala 2.12. Placed on a case class or a sealed trait, it adds an implicit val codecFor<Type> (or encode<Type> / decode<Type>, depending on its JsonDeriveConfig argument) to the companion object. The other annotations on the class still apply to the generated codec. On Scala 3, use a derives clause instead.

See @jsonDerive Macro Annotation for installation, the generated member names, generic classes and the limitations.

See Alsoโ€‹

ZIO Chat

๐ŸŒŸ Greetings from ZIO Chat! ๐ŸŒŸ Have questions about ZIO? Look no further! I'm here to be your ZIO guru. Whether you're a seasoned pro or just starting out, I've got answers tailored just for you. Fire away with your ZIO queries, and I'll hit you back with the best insights I've got. Let's dive into the world of ZIO together!