trait Codec[L, H, +CF <: CodecFormat] extends AnyRef

A bi-directional mapping between low-level values of type L and high-level values of type H. Low level values are formatted as CF.

The mapping consists of a pair of functions, one to decode (L => H), and one to encode (H => L). Decoding can fail, and this is represented as a result of type DecodeResult.

A codec also contains optional meta-data in the schema of the high-level value (which includes validators), as well as an instance of the format (which determines the media type of the low-level value).

Codec instances are used as implicit values, and are looked up when defining endpoint inputs/outputs. Depending on a particular endpoint input/output, it might require a codec which uses a specific format, or a specific low-level value.

Codec instances can be derived basing on other values (e.g. such as json encoders/decoders when integrating with json libraries). Or, they can be defined by hand for custom types, usually customising an existing, simpler codec.

Codecs can be chained with Mapping s using the map function.

L

The type of the low-level value.

H

The type of the high-level value.

CF

The format of encoded values. Corresponds to the media type.

Self Type
Codec[L, H, CF]
Annotations
@implicitNotFound( ... )
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Codec
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def encode(h: H): L
  2. abstract def format: CF
  3. abstract def rawDecode(l: L): DecodeResult[H]
  4. abstract def schema: Schema[H]

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native() @HotSpotIntrinsicCandidate()
  6. def decode(l: L): DecodeResult[H]

    • calls rawDecode
    • catches any exceptions that might occur, converting them to decode failures
    • validates the result
  7. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  9. def format[CF2 <: CodecFormat](f: CF2): Codec[L, H, CF2]
  10. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  11. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  12. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  13. def map[HH](f: (H) ⇒ HH)(g: (HH) ⇒ H): Codec[L, HH, CF]
  14. def map[HH](mapping: Mapping[H, HH]): Codec[L, HH, CF]
  15. def mapDecode[HH](f: (H) ⇒ DecodeResult[HH])(g: (HH) ⇒ H): Codec[L, HH, CF]
  16. def mapEither[HH](f: (H) ⇒ Either[String, HH])(g: (HH) ⇒ H): Codec[L, HH, CF]

    Maps this codec to the given higher-level type HH.

    Maps this codec to the given higher-level type HH.

    HH

    target type

    f

    decoding function

    g

    encoding function

    See also

    map

    mapDecode

    mapValidate

  17. def mapValidate[HH](v: Validator[H])(f: (H) ⇒ HH)(g: (HH) ⇒ H): Codec[L, HH, CF]

    Adds the given validator to the codec's schema, and maps this codec to the given higher-level type HH.

    Adds the given validator to the codec's schema, and maps this codec to the given higher-level type HH.

    Unlike a .validate(v).map(f)(g) invocation, during decoding the validator is run before applying the f function. If there are validation errors, decoding fails. However, the validator is then invoked again on the fully decoded value.

    This is useful to create codecs for types, which are unrepresentable unless the validator's condition is met, e.g. due to preconditions in the constructor.

    See also

    validate

  18. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  19. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  20. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  21. def schema(modify: (Schema[H]) ⇒ Schema[H]): Codec[L, H, CF]
  22. def schema(s2: Option[Schema[H]]): Codec[L, H, CF]
  23. def schema(s2: Schema[H]): Codec[L, H, CF]
  24. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  25. def toString(): String
    Definition Classes
    AnyRef → Any
  26. def validate(v: Validator[H]): Codec[L, H, CF]

    Adds a validator to the codec's schema.

    Adds a validator to the codec's schema.

    Note that validation is run on a fully decoded value. That is, during decoding, first the decoding functions are run, followed by validations. Hence any functions provided in subsequent .maps or .mapDecodes will be invoked before validation.

    See also

    mapValidate

  27. def validateIterable[C[X] <: Iterable[X], U](v: Validator[U])(implicit hIsCU: =:=[H, C[U]]): Codec[L, H, CF]

    Adds a validator which validates each element in the collection.

    Adds a validator which validates each element in the collection.

    Note that validation is run on a fully decoded value. That is, during decoding, first the decoding functions are run, followed by validations. Hence any functions provided in subsequent .maps or .mapDecodes will be invoked before validation.

    Should only be used if the schema hasn't been created by .mapping another one, but directly from Schema[U]. Otherwise the shape of the schema doesn't correspond to the type T, but to some lower-level representation of the type. This might cause invalid results at run-time.

  28. def validateOption[U](v: Validator[U])(implicit hIsOptionU: =:=[H, Option[U]]): Codec[L, H, CF]

    Adds a validator which validates the option's element, if it is present.

    Adds a validator which validates the option's element, if it is present.

    Note that validation is run on a fully decoded value. That is, during decoding, first the decoding functions are run, followed by validations. Hence any functions provided in subsequent .maps or .mapDecodes will be invoked before validation.

    Should only be used if the schema hasn't been created by .mapping another one, but directly from Schema[U]. Otherwise the shape of the schema doesn't correspond to the type T, but to some lower-level representation of the type. This might cause invalid results at run-time.

  29. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  30. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  31. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] ) @Deprecated
    Deprecated

Inherited from AnyRef

Inherited from Any

Ungrouped