JsonCodecDeriver

zio.blocks.schema.json.JsonCodecDeriver
See theJsonCodecDeriver companion class

Provides a default implementation of JsonCodecDeriver with customizable settings for JSON and binary codec derivation. This object allows the derivation of codecs for various data types, including primitives, records, variants, sequences, maps, and dynamic values.

The derivation process can be customized through pre-configured parameters, including:

  • fieldNameMapper: Controls how field names are transformed during serialization and deserialization.
  • caseNameMapper: Controls how case names in variants are transformed.
  • discriminatorKind: Determines the strategy for handling type discriminators in variants.
  • rejectExtraFields: Specifies if unrecognized fields should cause validation errors.
  • enumValuesAsStrings: Specifies whether enumeration values are represented as strings.
  • transientNone: Excludes fields with a value of None (for Option) or absent (for Maybe) during serialization.
  • requireOptionFields: Enforces the inclusion of optional fields (Option and Maybe) in deserialization.
  • transientEmptyCollection: Excludes empty collections during serialization.
  • requireCollectionFields: Enforces the inclusion of collection fields in deserialization.
  • transientDefaultValue: Excludes fields with default values during serialization.
  • requireDefaultValueFields: Enforces the inclusion of fields with default values in deserialization.

This predefined object uses the NameMapper.Identity strategy, which applies no transformation to field or case names, and DiscriminatorKind.Key, which embeds type information as a key in serialized data.

Attributes

Companion
class
Source
JsonCodecDeriver.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Type members

Inherited types

type HasInstance[F[_, _]] = HasInstance[F, JsonCodec]

Attributes

Inherited from:
Deriver
Source
Deriver.scala

Inherited and Abstract types

type Col[_]

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
type Elem

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
type Key

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
type Map[_, _]

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
type TC[_]

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
type Value

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
type Wrapped

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala

Value members

Inherited methods

final def binding[F[_, _], T, A](fa: F[T, A])(implicit F: HasBinding[F]): Binding[T, A]

Attributes

Inherited from:
Deriver
Source
Deriver.scala
override def deriveDynamic[F[_, _]](binding: Dynamic, doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[DynamicValue], examples: Seq[DynamicValue])(implicit F: HasBinding[F], D: HasInstance[F]): Lazy[JsonCodec[DynamicValue]]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
override def deriveMap[F[_, _], M[_, _], K, V](key: Reflect[F, K], value: Reflect[F, V], typeId: TypeId[M[K, V]], binding: Map[M, K, V], doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[M[K, V]], examples: Seq[M[K, V]])(implicit F: HasBinding[F], D: HasInstance[F]): Lazy[JsonCodec[M[K, V]]]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
override def derivePrimitive[A](primitiveType: PrimitiveType[A], typeId: TypeId[A], binding: Primitive[A], doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[A], examples: Seq[A]): Lazy[JsonCodec[A]]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
override def deriveRecord[F[_, _], A](fields: IndexedSeq[Term[F, A, _]], typeId: TypeId[A], binding: Record[A], doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[A], examples: Seq[A])(implicit F: HasBinding[F], D: HasInstance[F]): Lazy[JsonCodec[A]]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
override def deriveSequence[F[_, _], C[_], A](element: Reflect[F, A], typeId: TypeId[C[A]], binding: Seq[C, A], doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[C[A]], examples: Seq[C[A]])(implicit F: HasBinding[F], D: HasInstance[F]): Lazy[JsonCodec[C[A]]]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
override def deriveVariant[F[_, _], A](cases: IndexedSeq[Term[F, A, _]], typeId: TypeId[A], binding: Variant[A], doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[A], examples: Seq[A])(implicit F: HasBinding[F], D: HasInstance[F]): Lazy[JsonCodec[A]]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def deriveWrapper[F[_, _], A, B](wrapped: Reflect[F, B], typeId: TypeId[A], binding: Wrapper[A, B], doc: Doc, modifiers: Seq[Reflect], defaultValue: Option[A], examples: Seq[A])(implicit F: HasBinding[F], D: HasInstance[F]): Lazy[JsonCodec[A]]

Attributes

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
final def instance[F[_, _], T, A](fa: F[T, A])(implicit D: HasInstance[F]): Lazy[JsonCodec[A]]

Attributes

Inherited from:
Deriver
Source
Deriver.scala
override def instanceOverrides: IndexedSeq[InstanceOverride]

Attributes

Definition Classes
Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala

Attributes

Inherited from:
Deriver
Source
Deriver.scala

Updates the JsonCodecDeriver instance with the specified case name mapper. The case name mapper defines how case names should be transformed during encoding and decoding.

Updates the JsonCodecDeriver instance with the specified case name mapper. The case name mapper defines how case names should be transformed during encoding and decoding.

Value parameters

caseNameMapper

The NameMapper to apply for transforming case names.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated case name mapper.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala

Updates the JsonCodecDeriver instance with the specified discriminator kind. The discriminator kind defines how the subtype discriminator is represented in the serialized JSON.

Updates the JsonCodecDeriver instance with the specified discriminator kind. The discriminator kind defines how the subtype discriminator is represented in the serialized JSON.

Value parameters

discriminatorKind

The DiscriminatorKind to apply for specifying how the discriminator is handled in the JSON schema for sealed hierarchies.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated discriminator kind.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withEnumValuesAsStrings(enumValuesAsStrings: Boolean): JsonCodecDeriver

Updates the JsonCodecDeriver instance to specify whether enumeration values should be serialized and deserialized as strings.

Updates the JsonCodecDeriver instance to specify whether enumeration values should be serialized and deserialized as strings.

Value parameters

enumValuesAsStrings

A boolean flag indicating whether to treat enumeration values as strings. If true, enumeration values are serialized and deserialized as their string representations; if false, they are encoded with default DiscriminatorKind.Key encoding.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated enumValuesAsStrings setting.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala

Updates the JsonCodecDeriver instance with the specified field name mapper. The field name mapper defines how field names should be transformed during encoding and decoding.

Updates the JsonCodecDeriver instance with the specified field name mapper. The field name mapper defines how field names should be transformed during encoding and decoding.

Value parameters

fieldNameMapper

The NameMapper to apply for transforming field names.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated field name mapper.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
final def withInstance[A, B](typeId: TypeId[A], termName: String, instance: => JsonCodec[B]): Deriver[JsonCodec]

Returns a new deriver that pre-registers a field-level instance override. During derivation, the field named termName inside the parent record/variant identified by typeId will use the supplied instance instead of deriving one.

Returns a new deriver that pre-registers a field-level instance override. During derivation, the field named termName inside the parent record/variant identified by typeId will use the supplied instance instead of deriving one.

This is a medium-precision override between optic-based (exact path) and type-based (all occurrences).

Type parameters

A

the parent record or variant type that owns the field

B

the type of the field being overridden. Note that B is not statically checked against the actual field type; a mismatch will surface as a runtime error during codec use, not at derivation time.

Value parameters

instance

the custom type-class instance for the field (evaluated lazily)

termName

the name of the field (or variant case) to override. If no field with this name exists in the target type, the override is silently ignored during derivation (matching DerivationBuilder.instance behaviour).

typeId

identifier for the parent type A

Attributes

Returns

a new Deriver that wraps this deriver with the additional override

Inherited from:
Deriver
Source
Deriver.scala
final def withInstance[A](instance: => JsonCodec[A])(implicit typeId: TypeId[A]): Deriver[JsonCodec]

Returns a new deriver that pre-registers a type-level instance override. During derivation, every occurrence of type A will use the supplied instance instead of deriving one.

Returns a new deriver that pre-registers a type-level instance override. During derivation, every occurrence of type A will use the supplied instance instead of deriving one.

When the returned deriver is passed to DerivationBuilder, deriver-level instance overrides take precedence over builder-level instance overrides for the same target because DerivationBuilder.derive merges them as builderOverrides ++ deriver.instanceOverrides and later entries win in the resulting .toMap.

Type parameters

A

the type whose derived instance should be replaced

Value parameters

instance

the custom type-class instance (evaluated lazily)

typeId

implicit identifier for A, used to match occurrences during derivation

Attributes

Returns

a new Deriver that wraps this deriver with the additional override

Inherited from:
Deriver
Source
Deriver.scala
final def withModifier[A](typeId: TypeId[A], termName: String, modifier: Term): Deriver[JsonCodec]

Returns a new deriver that pre-registers a term-level modifier override. During derivation, the field named termName inside the parent type identified by typeId will have the modifier applied.

Returns a new deriver that pre-registers a term-level modifier override. During derivation, the field named termName inside the parent type identified by typeId will have the modifier applied.

Type parameters

A

the parent record or variant type that owns the field

Value parameters

modifier

the term-level modifier to apply

termName

the name of the field (or variant case) to modify. If no field with this name exists in the target type, the override is silently ignored.

typeId

identifier for A

Attributes

Returns

a new Deriver that wraps this deriver with the additional override

Inherited from:
Deriver
Source
Deriver.scala
final def withModifier[A](typeId: TypeId[A], modifier: Reflect): Deriver[JsonCodec]

Returns a new deriver that pre-registers a reflect-level modifier override. During derivation, every occurrence of the type identified by typeId will have the modifier prepended to its modifiers.

Returns a new deriver that pre-registers a reflect-level modifier override. During derivation, every occurrence of the type identified by typeId will have the modifier prepended to its modifiers.

Type parameters

A

the type to attach the modifier to

Value parameters

modifier

the reflect-level modifier to prepend

typeId

identifier for A

Attributes

Returns

a new Deriver that wraps this deriver with the additional override

Inherited from:
Deriver
Source
Deriver.scala
def withRejectExtraFields(rejectExtraFields: Boolean): JsonCodecDeriver

Updates the JsonCodecDeriver instance to specify whether additional fields in the JSON input that are not part of the schema should be rejected during decoding.

Updates the JsonCodecDeriver instance to specify whether additional fields in the JSON input that are not part of the schema should be rejected during decoding.

Value parameters

rejectExtraFields

A boolean flag indicating whether to reject extra fields that are not defined in the schema. If true, decoding will fail when extra fields are encountered; if false, extra fields will be ignored.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated rejectExtraFields setting.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withRequireCollectionFields(requireCollectionFields: Boolean): JsonCodecDeriver

Sets the flag indicating whether collection fields are required.

Sets the flag indicating whether collection fields are required.

Value parameters

requireCollectionFields

A boolean value specifying if collection fields should be required.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated configuration.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withRequireDefaultValueFields(requireDefaultValueFields: Boolean): JsonCodecDeriver

Sets the flag indicating whether fields with default values are required.

Sets the flag indicating whether fields with default values are required.

Value parameters

requireDefaultValueFields

A boolean flag indicating whether fields with default values should be required.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated configuration.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withRequireOptionFields(requireOptionFields: Boolean): JsonCodecDeriver

Sets the requirement for optional fields (Option and Maybe).

Sets the requirement for optional fields (Option and Maybe).

Value parameters

requireOptionFields

A boolean flag indicating whether optional fields (Option and Maybe) are required. If true, these fields must be present in the JSON input and will not be treated as optional during codec derivation.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated setting for requiring optional fields.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withTransientDefaultValue(transientDefaultValue: Boolean): JsonCodecDeriver

Sets the transient behavior for fields with defined default values.

Sets the transient behavior for fields with defined default values.

Value parameters

transientDefaultValue

A boolean indicating whether the transient behavior for fields with defined default values should be applied or not.

Attributes

Returns

A new instance of JsonCodecDeriver with the specified transient default value setting.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withTransientEmptyCollection(transientEmptyCollection: Boolean): JsonCodecDeriver

Configures whether the derived codec should handle empty collections as transient.

Configures whether the derived codec should handle empty collections as transient.

Value parameters

transientEmptyCollection

Indicates if empty collections should be treated as transient.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated transientEmptyCollection setting.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala
def withTransientNone(transientNone: Boolean): JsonCodecDeriver

Updates the JsonCodecDeriver instance to specify whether fields of type Option with a value of None or Maybe with an absent value should be excluded during encoding.

Updates the JsonCodecDeriver instance to specify whether fields of type Option with a value of None or Maybe with an absent value should be excluded during encoding.

Value parameters

transientNone

A boolean flag indicating whether to exclude fields of type Option with a value of None (or Maybe with an absent value) during encoding. If true, such fields are omitted; if false, they are included.

Attributes

Returns

A new instance of JsonCodecDeriver with the updated transientNone setting.

Inherited from:
JsonCodecDeriver
Source
JsonCodecDeriver.scala