Bidirectional byte codec combining encoder and decoder.
ByteCodec extends both ByteEncoder and ByteDecoder, providing a single type class for types that support both encoding and decoding.
Most types have both encoder and decoder instances, so ByteCodec is automatically derived when both are available.
Attributes
- See also
-
ByteEncoder for encoding operations
ByteDecoder for decoding operations
types.md for encoding/decoding rules per type
- Companion
- object
- Source
- ByteCodec.scala
- Graph
-
- Supertypes
- Known subtypes
-
trait OrderedCodec[A]
- Self type
-
ByteCodec[A]
Members list
Value members
Concrete methods
Maps a codec through an isomorphism.
Maps a codec through an isomorphism.
Useful for opaque wrappers whose representation already has a codec.
Type parameters
- B
-
the target type
Value parameters
- from
-
conversion from B to A
- to
-
conversion from A to B
Attributes
- Returns
-
a new ByteCodec for type B
- Source
- ByteCodec.scala
Inherited methods
Creates a new encoder by applying a function before encoding.
Creates a new encoder by applying a function before encoding.
This is the contravariant map operation for encoders.
Value parameters
- f
-
function to transform input before encoding
Attributes
- Returns
-
new encoder for type B
- Example
-
case class UserId(value: Long) given ByteEncoder[UserId] = ByteEncoder[Long].contramap(_.value) - Inherited from:
- ByteEncoder
- Source
- ByteEncoder.scala
Creates a new decoder with validation.
Creates a new decoder with validation.
Value parameters
- f
-
function that may fail with DecodeFailure
Attributes
- Returns
-
new decoder for type B
- Example
-
val positiveDecoder = ByteDecoder[Long].emap { n => Either.cond(n > 0, n, DecodeFailure("Must be positive")) } - Inherited from:
- ByteDecoder
- Source
- ByteDecoder.scala
Creates a new decoder by chaining decoders.
Creates a new decoder by chaining decoders.
Value parameters
- f
-
function that creates a decoder based on decoded value
Attributes
- Returns
-
new decoder for type B
- Inherited from:
- ByteDecoder
- Source
- ByteDecoder.scala
Creates a new decoder by transforming the decoded value.
Creates a new decoder by transforming the decoded value.
Value parameters
- f
-
function to transform decoded value
Attributes
- Returns
-
new decoder for type B
- Inherited from:
- ByteDecoder
- Source
- ByteDecoder.scala
Widens the decoder to a supertype.
Widens the decoder to a supertype.
Type parameters
- AA
-
the supertype
Attributes
- Returns
-
a decoder for the wider type
- Inherited from:
- ByteDecoder
- Source
- ByteDecoder.scala
Inherited and Abstract methods
Decodes bytes to a value.
Decodes bytes to a value.
Value parameters
- bytes
-
the byte sequence to decode
Attributes
- Returns
-
Either failure or decoded value with remainder
- Inherited from:
- ByteDecoder
- Source
- ByteDecoder.scala
Encodes a value to a deterministic byte sequence.
Encodes a value to a deterministic byte sequence.
Value parameters
- value
-
the value to encode
Attributes
- Returns
-
the encoded byte sequence
- Inherited from:
- ByteEncoder
- Source
- ByteEncoder.scala