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
2
votes
1 answer

Can't find scodec-akka published to Maven Central

I need to convert the ByteString received by AKKA Streams to BitVector int order to decode it by scodec. The library is said to be be published to Maven central under org.scodec %% scodec-akka as mentioned here GitHub scodec-akka so i need to know…
Assem-Hafez
  • 1,045
  • 10
  • 11
2
votes
1 answer

define `Codec` for recursive data structure

I have a class looking like this, case class Foo ( bar: Int, foos: Vector[Foo] ) to define a Codec[Foo], I tried this, def fc = shapeless.Lazy((int32 ~~ vector(fc)).widenOpt( (Foo.apply _).tupled, Foo.unapply _ )) But this did not work, since…
Sheng
  • 1,697
  • 4
  • 19
  • 33
2
votes
1 answer

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

I am trying to define an Scodec coproduct codec for communicating with an EELink GPS. Here is the code: import scodec.Codec import scodec.bits.ByteVector import scodec.codecs._ trait Message object Message { implicit val discriminated:…
Mario Camou
  • 2,303
  • 16
  • 28
2
votes
1 answer

Using nested case classes with scodec

When defining my messages with scodec, I would like to use nested case classes. For example: case class Foo(x: Int, y: Int) object Foo { def baseCodec = uint16 :: uint16 def codec = baseCodec.as[Foo] } case class Bar(a: Int, foo: Foo, b:…
Mario Camou
  • 2,303
  • 16
  • 28
2
votes
1 answer

.dropUnit on .hlist'ed codecs doesnt seem to work

For "big" codecs, the Scala phase typer takes forever (we're talking minutes) when creating a codec directly from HLists and applying .dropUnits ( ignore(6) :: uint(2) :: uint(30) :: int(4) :: int(8) :: uint(10) :: bool(1) :: int(28)…
kareblak
  • 412
  • 1
  • 3
  • 12
2
votes
1 answer

scodec decode/encode split length field

I am using scodec: https://github.com/scodec/scodec to decode/encode a binary protocol. I am struggling with a part of the spec where a "length" field is split into two parts by a "moreflag". The moreflag indicates if the length field needs more…
Chris Ridmann
  • 2,836
  • 4
  • 18
  • 19
1
vote
1 answer

How to write scodec codec for Collection Map

I have below case class case class Foo(code: Int, msg: String, headers: Map[String,String] = Map.empty) Below is the code that I've tried so far - import scodec._ import scodec.codecs._ implicit val mapCodec: Codec[List[(String, String)]] =…
user51
  • 8,843
  • 21
  • 79
  • 158
1
vote
1 answer

How to dynamically provide N codecs to process fields as a VectorCodec for a record of binary fields that do not contain size bytes

Considering this function in Decoder: final def decodeCollect[F[_], A](dec: Decoder[A], limit: Option[Int])(buffer: BitVector)(implicit cbf: Factory[A, F[A]]): Attempt[DecodeResult[F[A]]] = { What I really need is dec: Vector[Decoder[A]], like…
Mike Jones
  • 51
  • 2
1
vote
2 answers

Scodec: Using vectorOfN with a vlong field

I am playing around with the Bitcoin blockchain to learn Scala and some useful libraries. Currently I am trying to decode and encode Blocks with SCodec and my problem is that the vectorOfN function takes its size as an Int. How can I use a long…
icehawk
  • 1,145
  • 3
  • 12
  • 21
1
vote
0 answers

Byte align BitVector after decoding misaligned value

I've come across an interesting problem with scodec. I have a peculiar encoding scheme that requires a stream byte alignment when the current bit pointer mod 8 is not zero (not aligned to the nearest byte). Now normally, this would be handled by the…
RAX
  • 63
  • 6
1
vote
1 answer

Schema of codec is part of the data stream

I'm currently evaluating if scodec is the right tool for my task. I have to parse an InputStream (file or network) which is structured the following: | Header - FieldDesc1 - FieldDesc2 - ... \ - FieldDescM - Record1 - Record2 - ... - RecordN…
Joachim Rosskopf
  • 1,259
  • 2
  • 13
  • 24
0
votes
1 answer

scodec - Method too large

Have a sealed trait DataTag and its subtypes (86 case classes). After upgrade project to scala 2.12, scodec-core 1.11.8 and scodec-bits 1.1.27, compilation fail with [error] Method too large: protocol/Payload/BasePacket$.tagCodec…
0
votes
0 answers

shapeless.~?>.witness[Nothing, Nothing, T](shapeless.~?>.rel[Nothing, Nothing])when upgrading scala version from 2 to 3

I'm upgrading my scala project from version 2 to 3. This is my build.sbt for version scala version 2 scalaVersion := "2.13.5" version := "1.0.0" libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.7" libraryDependencies +=…
user51
  • 8,843
  • 21
  • 79
  • 158
0
votes
1 answer

Scodec - Reading in a fixed-length String

I'm writing a file parser that is reading an existing file format that incorporates fixed length, 0 padded strings. So, for example I've got two case classes for binary structures within the file I need to parse. The first includes a 4-character…
Adam Luchjenbroers
  • 4,917
  • 2
  • 30
  • 35
0
votes
1 answer

Could not prove that Long :: String :: Option[Long] :: Option[java.util.UUID] :: shapeless.HNil can be converted to/from SimpleMp4BoxHeader

i am a starter and working on MP4 file decoding.when i code as the following, it reports "Could not prove that Long :: String :: Option[Long] :: Option[java.util.UUID] :: shapeless.HNil can be converted to/from SimpleMp4BoxHeader". how can i fix…
ruoyu
  • 3
  • 1