Questions tagged [scodec]

scodec is a pure functional Scala library that allows encoding/decoding of binary data.

scodec is a suite of Scala combinator libraries for working with binary data. Support ranges from simple, performant data structures for working with bits and bytes to streaming encoding and decoding.

This library focuses on contract-first and pure functional encoding and decoding of binary data. The following design constraints are considered:

  • Binary structure should mirror protocol definitions and be self-evident under casual reading
  • Mapping binary structures to types should be statically verified
  • Encoding and decoding should be purely functional
  • Failures in encoding and decoding should provide descriptive errors Compiler plugin should not be used

As a result, the library is implemented as a combinator based DSL. Performance is considered but yields to the above design constraints.

A good place to start using it is to look at the pre-existing codecs and how they can be combined by checking out the available operators.

Links: scodec.org and github.

35 questions
6
votes
1 answer

Scodec combinators: Header contains magic number that is used to discriminate types

I am looking for a way to approach a protocol like the following example: case class Request(bodyType: Int, foo: Int, bar: Int, body: RequestBody) sealed trait RequestBody case class Read(key: String) extends RequestBody case class Write(key:…
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
6
votes
1 answer

Shapeless: Inversion of filterNot on an HList

I'm trying to write a combinator for the scodec library that converts a Codec[K] in to a Codec[L] where K is an HList and L is the equivalent HList with all Unit elements removed. Implementing decoding can be done by decoding a K and then filtering…
mpilquist
  • 3,855
  • 21
  • 22
5
votes
1 answer

Flatten an arbitrarily nested codec?

As a new user of SCodec, there is quite a learning curve. I've hit a snag that I can't seem to solve despite reading the source and docs. I want to be able to define popular codecs as functions like this def packedByte : Codec[Int :: Int :: Int ::…
RAX
  • 63
  • 6
4
votes
1 answer

Scodec - Coproducts could not find implicit value for parameter auto: scodec.codecs.CoproductBuilderAuto

On version: "org.typelevel" %% "scodec-core" % "1.5.0" I'm trying to use coproduct functionality, as shown in the test case demonstrate fixing the codec to a known subtype. I keep getting the error: "could not find implicit value for parameter…
Chris Ridmann
  • 2,836
  • 4
  • 18
  • 19
3
votes
0 answers

How do I define value of discriminator for coproduct codecs?

Could you please explain how coproducts really work? Here is my code sealed trait ArdbData case class ArdbDataString(value: String) extends ArdbData case class ArdbDataLong(value: Long) extends ArdbData case class ArdbDataDouble(value: Double)…
expert
  • 29,290
  • 30
  • 110
  • 214
3
votes
1 answer

Transforming a Hlist[F[A]] into F[B] where case class B has aligned types with A

Note: I am learning shapeless, so please ask for clarification if I miss any details. Background: I am building an encoding/decoding solution for fixed-length format while practising Shapeless. The idea is that each case class would have its own…
Atais
  • 10,857
  • 6
  • 71
  • 111
3
votes
1 answer

scodec variableSizePrefixBytes transformation

I have a use case where a header can contain 7 bytes plus an optional 0-15 Bytes of information where the size information is in the lower 4 bits of the 5th Byte so the format is: 4 bytes | 4 bits | 4 bits <- length of extra bytes | 2 bytes | 0-15…
Gertjan Assies
  • 1,890
  • 13
  • 23
3
votes
1 answer

Scodec: How to create a codec for an optional byte

I must create a codec for a message that has the following specification The message length is indicated by a byte of which the least significant bit is an extension flag that, when set indicates that the following (optional) byte must be used as…
iandebeer
  • 227
  • 2
  • 8
3
votes
2 answers

Mapping C structs in Scala

What is the best way to read and write C-styled byte stuctures in Scala, like this: struct account { int id; char[10] data1; char[10] data2; float dataFloat; }; There's unpack function in…
Alexander Teut
  • 323
  • 1
  • 3
  • 14
3
votes
1 answer

scodec ignore last value in codec conversion between hlist and case class

I'm just starting out with typelevel's "scodec" library: https://github.com/scodec/scodec I've found that I've been using the following function a lot: /** * When called on a `Codec[L]` for some `L <: HList`, returns a new codec that…
Chris Ridmann
  • 2,836
  • 4
  • 18
  • 19
2
votes
0 answers

How can I Throw error inside scodec Attempt?

I have the following code to decode a BitVector. How can I remove the exception and throw a failure? def decode(b: BitVector) = { if (!applies) { Attempt.successful(DecodeResult.apply(null, b)) } else { string.decode(b) match { …
mat656
  • 129
  • 1
  • 7
2
votes
1 answer

Missing scodec.Codec[Command] implicit because of class with non-value fields

I'm trying to use discriminators in existing project and something is wrong with my classes I guess. Consider this scodec example. If I change TurnLeft and its codec to sealed class TurnLeft(degrees: Int) extends Command { def getDegrees: Int =…
expert
  • 29,290
  • 30
  • 110
  • 214
2
votes
2 answers

Records are discriminated hierarchically

I have to implement some proprietary binary format and wanted to do this with scodec. However, I cannot find a concise solution. The format is as follows: A file consists of multiple Records, where each record is prefixed with a little endian 16-bit…
VictorS
  • 41
  • 4
2
votes
2 answers

Encoding vector length field not adjacent to the vector

I have the following structure I like to encode. I'm aware that I can encode a vector with vector() if the size field is directly in front of the vector data. But here the field encoding the vector size is not adjacent. case class Item( address:…
revau.lt
  • 2,674
  • 2
  • 20
  • 31
2
votes
1 answer

Efficiently pack a list of Longs in Scodec representation

I have a case class with a List[Long] attribute that I am converting into a token using the Scodec library. Right now, it is not efficient (space-wise) because I am using this codec: listOfN(uint16, int64) This is using all 64 bits even though my…
Neel Kumar
  • 180
  • 1
  • 9
1
2 3