trait Codec[A] extends GenCodec[A, A]
Supports encoding a value of type A
to a BitVector
and decoding a BitVector
to a value of A
.
Not every value of A
can be encoded to a bit vector and similarly, not every bit vector can be decoded to a value
of type A
. Hence, both encode and decode return either an error or the result. Furthermore, decode returns the
remaining bits in the bit vector that it did not use in decoding.
There are various ways to create instances of Codec
. The trait can be implemented directly or one of the
constructor methods in the companion can be used (e.g., apply
). Most of the methods on Codec
create return a new codec that has been transformed in some way. For example, the xmap method
converts a Codec[A]
to a Codec[B]
given two functions, A => B
and B => A
.
One of the simplest transformation methods is def withContext(context: String): Codec[A]
, which
pushes the specified context string in to any errors (i.e., Err
s) returned from encode or decode.
See the methods on this trait for additional transformation types.
See the codecs package object for pre-defined codecs for many common data types and combinators for building larger codecs out of smaller ones.
Tuple Codecs
The ~
operator supports combining a Codec[A]
and a Codec[B]
in to a Codec[(A, B)]
.
For example:
val codec: Codec[Int ~ Int ~ Int] = uint8 ~ uint8 ~ uint8
Codecs generated with ~
result in left nested tuples. These left nested tuples can
be pulled back apart by pattern matching with ~
. For example:
Codec.decode(uint8 ~ uint8 ~ uint8, bytes) map { case a ~ b ~ c => a + b + c }
Alternatively, a function of N arguments can be lifted to a function of left-nested tuples. For example:
val add3 = (_: Int) + (_: Int) + (_: Int) Codec.decode(uint8 ~ uint8 ~ uint8, bytes) map add3
Similarly, a left nested tuple can be created with the ~
operator. This is useful when creating the tuple structure
to pass to encode. For example:
(uint8 ~ uint8 ~ uint8).encode(1 ~ 2 ~ 3)
Tuple based codecs are of limited use compared to HList
based codecs, which is discussed later.
Note: this design is heavily based on Scala's parser combinator library and the syntax it provides.
flatZip
Sometimes when combining codecs, a latter codec depends on a formerly decoded value.
The flatZip
method is important in these types of situations -- it represents a dependency between
the left hand side and right hand side. Its signature is def flatZip[B](f: A => Codec[B]): Codec[(A, B)]
.
This is similar to flatMap
except the return type is Codec[(A, B)]
instead of Decoder[B]
.
Consider a binary format of an 8-bit unsigned integer indicating the number of bytes following it.
To implement this with flatZip
, we could write:
val x: Codec[(Int, ByteVector)] = uint8 flatZip { numBytes => bytes(numBytes) } val y: Codec[ByteVector] = x.xmap[ByteVector]({ case (_, bv) => bv }, bv => (bv.size, bv))
In this example, x
is a Codec[(Int, ByteVector)]
but we do not need the size directly in the model
because it is redundant with the size stored in the ByteVector
. Hence, we remove the Int
by
xmap
-ping over x
. The notion of removing redundant data from models comes up frequently.
Note: there is a combinator that expresses this pattern more succinctly -- variableSizeBytes(uint8, bytes)
.
HList Codecs
HList
s are similar to tuples in that they represent the product of an arbitrary number of types. That is,
the size of an HList
is known at compile time and the type of each element is also known at compile time.
For more information on HList
s in general, see Shapeless.
Codec
makes heavy use of HList
s. The primary operation is extending a Codec[L]
for some L <: HList
to
a Codec[A :: L]
. For example:
val uint8: Codec[Int] = ... val string: Codec[String] = ... val codec: Codec[Int :: Int :: String] = uint8 :: uint8 :: string
The ::
method is sort of like cons-ing on to the HList
but it is doing so *inside* the Codec
type.
The resulting codec encodes values by passing each component of the HList
to the corresponding codec
and concatenating all of the results.
There are various methods on this trait that only work on Codec[L]
for some L <: HList
. Besides the aforementioned
::
method, there are others like :::
, flatPrepend
, flatConcat
, etc. One particularly useful method is
dropUnits
, which removes any Unit
values from the HList
.
Given a Codec[X0 :: X1 :: ... Xn :: HNil]
and a case class with types X0
to Xn
in the same order,
the HList
codec can be turned in to a case class codec via the as
method. For example:
case class Point(x: Int, y: Int, z: Int) val threeInts: Codec[Int :: Int :: Int :: HNil] = uint8 :: uint8 :: uint8 val point: Codec[Point] = threeInts.as[Point]
flatPrepend
The HList
analog to flatZip
is flatPrepend
. It has the signature:
def flatPrepend[L <: HList](f: A => Codec[L]): Codec[A :: L]
It forms a codec of A
consed on to L
when called on a Codec[A]
and passed a function A => Codec[L]
.
Note that the specified function must return an HList
based codec. Implementing our example from earlier
using flatPrepend
:
val x: Codec[Int :: ByteVector :: HNil] = uint8 flatPrepend { numBytes => bytes(numBytes).hlist }
In this example, bytes(numBytes)
returns a Codec[ByteVector]
so we called .hlist
on it to lift it
in to a Codec[ByteVector :: HNil]
.
There are similar methods for flat appending and flat concating.
Coproduct Codecs
Given some ordered list of types, potentially with duplicates, a value of the HList
of those types
has a value for *every* type in the list. In other words, an HList
represents having an X0
AND X1
AND
... AND XN
. A Coproduct
for the same list of types represents having a value for *one* of those types.
In other words, a Coproduct
represents having an X0
OR X1
OR ... OR XN
. This is somewhat imprecise
because a coproduct can tell us exactly which Xi
we have, even in the presence of duplicate types.
A coproduct can also be thought of as an Either
that has an unlimited number of choices instead of just 2 choices.
Shapeless represents coproducts in a similar way as HList
s. A coproduct type is built using the :+:
operator
with a sentinal value of CNil
. For example, an Int
or Long
or String
is represented as the coproduct type:
Int :+: Long :+: String :+: CNil
For more information on coproducts in general, see Shapeless.
Like HList
based codecs, scodec supports Coproduct
based codecs by coopting syntax from Shapeless. Specifically,
the :+:
operator is used:
val builder = uint8 :+: int64 :+: utf8
Unlike HList
based codecs, the result of :+:
is not a codec but rather a codecs.CoproductCodecBuilder.
Having a list of types and a codec for each is not sufficient to build a coproduct codec. We also need to describe
how each entry in the coproduct is differentiated from the other entries. There are a number of ways to do this
and each way changes the binary format significantly. See the docs on CoproductCodecBuilder
for details.
Derived Codecs
Codecs for case classes and sealed class hierarchies can often be automatically derived.
Consider this example:
import scodec.codecs.implicits._ case class Point(x: Int, y: Int, z: Int) Codec[Point].encode(Point(1, 2, 3))
In this example, no explicit codec was defined for Point
yet Codec[Point]
successfully created one.
It did this by "reflecting" over the structure of Point
and looking up a codec for each component type
(note: no runtime reflection is performed - rather, this is implemented using macro-based compile time reflection).
In this case, there are three components, each of type Int
, so the compiler first looked for an implicit Codec[Int]
.
It then combined each Codec[Int]
using an HList
based codec and finally converted the HList
codec
to a Codec[Point]
. It found the implicit Codec[Int]
instances due to the import of scodec.codecs.implicits._
.
Furthermore, if there was an error encoding or decoding a field, the field name (i.e., x, y, or z) is included
as context on the Err
returned.
This works similarly for sealed class hierarchies -- each subtype is internally represented as a member of a coproduct. There must be the following implicits in scope however:
Discriminated[A, D]
for some discriminator typeD
, which provides theCodec[D]
to use for encoding/decoding the discriminatorDiscriminator[A, X, D]
for each subtypeX
ofA
, which provides the discriminator value for typeX
Codec[X]
for each subtypeX
ofA
Full examples are available in the test directory of this project.
Implicit Codecs
If authoring combinators that require implicit codec arguments, use shapeless.Lazy[Codec[A]]
instead of
Codec[A]
. This prevents the occurrence of diverging implicit expansion errors.
- Self Type
- Codec[A]
- Source
- Codec.scala
- Alphabetic
- By Inheritance
- Codec
- GenCodec
- Decoder
- Encoder
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
-
abstract
def
decode(bits: BitVector): Attempt[DecodeResult[A]]
Attempts to decode a value of type
A
from the specified bit vector.Attempts to decode a value of type
A
from the specified bit vector.- bits
bits to decode
- returns
error if value could not be decoded or the remaining bits and the decoded value
- Definition Classes
- Decoder
-
abstract
def
encode(value: A): Attempt[BitVector]
Attempts to encode the specified value in to a bit vector.
Attempts to encode the specified value in to a bit vector.
- value
value to encode
- returns
error or binary encoding of the value
- Definition Classes
- Encoder
-
abstract
def
sizeBound: SizeBound
Provides a bound on the size of successfully encoded values.
Provides a bound on the size of successfully encoded values.
- Definition Classes
- Encoder
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
def
:+:[B](left: Codec[B]): CoproductCodecBuilder[:+:[B, :+:[A, CNil]], ::[Codec[B], ::[Codec[A], HNil]], :+:[B, :+:[A, CNil]]]
Supports creation of a coproduct codec.
Supports creation of a coproduct codec. See scodec.codecs.CoproductCodecBuilder for details.
-
final
def
<~[B](codecB: Codec[B])(implicit ev: =:=[Unit, B]): Codec[A]
Assuming
B
isUnit
, creates aCodec[A]
that: encodes theA
followed by a unit; decodes anA
followed by a unit and discards the decoded unit.Assuming
B
isUnit
, creates aCodec[A]
that: encodes theA
followed by a unit; decodes anA
followed by a unit and discards the decoded unit.Operator alias of dropRight.
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
>>~[B](f: (A) ⇒ Codec[B]): Codec[(A, B)]
Returns a new codec that encodes/decodes a value of type
(A, B)
where the codec ofB
is dependent onA
.Returns a new codec that encodes/decodes a value of type
(A, B)
where the codec ofB
is dependent onA
. Operator alias for flatZip. -
def
asDecoder: Decoder[A]
Gets this as a
Decoder
.Gets this as a
Decoder
.- Definition Classes
- Decoder
-
def
asEncoder: Encoder[A]
Gets this as an
Encoder
.Gets this as an
Encoder
.- Definition Classes
- Encoder
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
compact: Codec[A]
Converts this codec to a new codec that compacts the encoded bit vector before returning it.
-
final
def
complete: Codec[A]
Converts this codec to a new codec that fails decoding if there are remaining bits.
-
final
def
consume[B](f: (A) ⇒ Codec[B])(g: (B) ⇒ A): Codec[B]
Similar to
flatZip
except theA
type is not visible in the resulting type -- the binary effects of theCodec[A]
still occur though.Similar to
flatZip
except theA
type is not visible in the resulting type -- the binary effects of theCodec[A]
still occur though.Example usage:
case class Flags(x: Boolean, y: Boolean, z: Boolean) (bool :: bool :: bool :: ignore(5)).consume { flgs => conditional(flgs.x, uint8) :: conditional(flgs.y, uint8) :: conditional(flgs.z, uint8) } { case x :: y :: z :: HNil => Flags(x.isDefined, y.isDefined, z.isDefined) } }
Note that when
B
is anHList
, this method is equivalent to usingflatPrepend
andderive
. That is,a.consume(f)(g) === a.flatPrepend(f).derive[A].from(g)
. -
def
contramap[C](f: (C) ⇒ A): GenCodec[C, A]
Converts this
GenCodec
to aGenCodec[C, B]
using the suppliedC => A
. -
def
decodeOnly[AA >: A]: Codec[AA]
Converts this to a codec that fails encoding with an error.
-
final
def
decodeValue(bits: BitVector): Attempt[A]
Attempts to decode a value of type
A
from the specified bit vector and discards the remaining bits.Attempts to decode a value of type
A
from the specified bit vector and discards the remaining bits.- bits
bits to decode
- returns
error if value could not be decoded or the decoded value
- Definition Classes
- Decoder
-
final
def
downcast[B <: A](implicit tb: Typeable[B]): Codec[B]
Safely lifts this codec to a codec of a subtype.
Safely lifts this codec to a codec of a subtype.
When a supertype of
B
that is not a supertype ofA
is decoded, an decoding error is returned. -
final
def
dropLeft[B](codecB: Codec[B])(implicit ev: =:=[Unit, A]): Codec[B]
Assuming
A
isUnit
, creates aCodec[B]
that: encodes the unit followed by aB
; decodes a unit followed by aB
and discards the decoded unit. -
final
def
dropRight[B](codecB: Codec[B])(implicit ev: =:=[Unit, B]): Codec[A]
Assuming
B
isUnit
, creates aCodec[A]
that: encodes theA
followed by a unit; decodes anA
followed by a unit and discards the decoded unit. -
def
econtramap[C](f: (C) ⇒ Attempt[A]): GenCodec[C, A]
Converts this
GenCodec
to aGenCodec[C, B]
using the suppliedC => Attempt[A]
. -
def
emap[C](f: (A) ⇒ Attempt[C]): GenCodec[A, C]
Converts this
GenCodec
to aGenCodec[A, C]
using the suppliedB => Attempt[C]
. -
def
encodeOnly: Codec[A]
Converts this to a codec that fails decoding with an error.
Converts this to a codec that fails decoding with an error.
- Definition Classes
- Encoder
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
exmap[B](f: (A) ⇒ Attempt[B], g: (B) ⇒ Attempt[A]): Codec[B]
Transforms using two functions,
A => Attempt[B]
andB => Attempt[A]
. -
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
flatMap[B](f: (A) ⇒ Decoder[B]): Decoder[B]
Converts this decoder to a
Decoder[B]
using the suppliedA => Decoder[B]
.Converts this decoder to a
Decoder[B]
using the suppliedA => Decoder[B]
.- Definition Classes
- Decoder
-
final
def
flatZip[B](f: (A) ⇒ Codec[B]): Codec[(A, B)]
Returns a new codec that encodes/decodes a value of type
(A, B)
where the codec ofB
is dependent onA
. -
final
def
flattenLeftPairs(implicit f: FlattenLeftPairs[A]): Codec[Out]
Converts this codec to an
HList
based codec by flattening all left nested pairs.Converts this codec to an
HList
based codec by flattening all left nested pairs. For example,flattenLeftPairs
on aCodec[(((A, B), C), D)]
results in aCodec[A :: B :: C :: D :: HNil]
. This is particularly useful when combined with~
,~>
, and<~
. -
final
def
fuse[AA <: A, BB >: A](implicit ev: =:=[BB, AA]): Codec[BB]
Converts this generalized codec in to a non-generalized codec assuming
A
andB
are the same type.Converts this generalized codec in to a non-generalized codec assuming
A
andB
are the same type.- Definition Classes
- GenCodec
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
hlist: Codec[::[A, HNil]]
Lifts this codec in to a codec of a singleton hlist.
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
map[C](f: (A) ⇒ C): GenCodec[A, C]
Converts this
GenCodec
to aGenCodec[A, C]
using the suppliedB => C
. -
final
def
narrow[B](f: (A) ⇒ Attempt[B], g: (B) ⇒ A): Codec[B]
Transforms using two functions,
A => Attempt[B]
andB => A
.Transforms using two functions,
A => Attempt[B]
andB => A
.The supplied functions form an injection from
B
toA
. Hence, this method converts from a larger to a smaller type. Hence, the namenarrow
. -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
pairedWith[B](codecB: Codec[B]): Codec[(A, B)]
Creates a
Codec[(A, B)]
that first encodes/decodes anA
followed by aB
. -
def
pcontramap[C](f: (C) ⇒ Option[A]): GenCodec[C, A]
Converts this
GenCodec
to aGenCodec[C, B]
using the supplied partial function fromC
toA
. -
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toField[K]: Codec[FieldType[K, A]]
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions.
-
def
toFieldWithContext[K <: Symbol](k: K): Codec[FieldType[K, A]]
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions.
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions. The specified key is pushed in to the context of any errors that are returned from the resulting codec.
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
tuple: Codec[::[A, HNil]]
Alias for
.hlist
- allows cross compilation with Scodec 2 / Scala 3. -
final
def
unit(zero: A): Codec[Unit]
Converts this to a
Codec[Unit]
that encodes using the specified zero value and decodes a unit value when this codec decodes anA
successfully. -
final
def
upcast[B >: A](implicit ta: Typeable[A]): Codec[B]
Safely lifts this codec to a codec of a supertype.
Safely lifts this codec to a codec of a supertype.
When a subtype of
B
that is not a subtype ofA
is passed to encode, an encoding error is returned. -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
widen[B](f: (A) ⇒ B, g: (B) ⇒ Attempt[A]): Codec[B]
Transforms using two functions,
A => B
andB => Attempt[A]
.Transforms using two functions,
A => B
andB => Attempt[A]
.The supplied functions form an injection from
A
toB
. Hence, this method converts from a smaller to a larger type. Hence, the namewiden
. -
final
def
withContext(context: String): Codec[A]
Creates a new codec that is functionally equivalent to this codec but pushes the specified context string in to any errors returned from encode or decode.
-
final
def
withToString(str: ⇒ String): Codec[A]
Creates a new codec that is functionally equivalent to this codec but returns the specified string from
toString
. -
final
def
xmap[B](f: (A) ⇒ B, g: (B) ⇒ A): Codec[B]
Transforms using the isomorphism described by two functions,
A => B
andB => A
. -
final
def
~[B](codecB: Codec[B]): Codec[(A, B)]
Creates a
Codec[(A, B)]
that first encodes/decodes anA
followed by aB
.Creates a
Codec[(A, B)]
that first encodes/decodes anA
followed by aB
.Operator alias for pairedWith.
-
final
def
~>[B](codecB: Codec[B])(implicit ev: =:=[Unit, A]): Codec[B]
Assuming
A
isUnit
, creates aCodec[B]
that: encodes the unit followed by aB
; decodes a unit followed by aB
and discards the decoded unit.Assuming
A
isUnit
, creates aCodec[B]
that: encodes the unit followed by aB
; decodes a unit followed by aB
and discards the decoded unit.Operator alias of dropLeft.