Code Monkey home page Code Monkey logo

scodec's Introduction

scodec

Gitter

Scala combinator library for working with binary data.

Design Constraints

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.

Acknowledgements

The library uses Shapeless and is heavily influenced by scala.util.parsing.combinator.

Administrative

This project is licensed under a 3-clause BSD license.

The scodec mailing list contains release announcements and is generally a good place to go for help. Also consider using the scodec tag on StackOverflow.

People are expected to follow the Typelevel Code of Conduct when discussing scodec on the Github page, Gitter channel, mailing list, or other venues.

Concerns or issues can be sent to Michael Pilquist ([email protected]) or to Typelevel.

Introduction

The primary abstraction is a Codec[A], which supports encoding a value of type A to a BitVector and decoding a BitVector to a value of type A.

The codecs package provides a number of predefined codecs and combinators.

    import scodec._
    import scodec.bits._
    import codecs._

    // Create a codec for an 8-bit unsigned int followed by an 8-bit unsigned int followed by a 16-bit unsigned int
    val firstCodec = uint8 ~ uint8 ~ uint16

    // Decode a bit vector using that codec
    val result: Attempt[DecodeResult[(Int ~ Int ~ Int)]] = firstCodec.decode(hex"102a03ff".bits)
    // Successful(DecodeResult(((16, 42), 1023), BitVector(empty)))

    // Sum the result
    val add3 = (_: Int) + (_: Int) + (_: Int)
    val sum: Attempt[DecodeResult[Int]] = result.map(_.map(add3))
    // Successful(DecodeResult(1081, BitVector(empty)))

Automatic case class binding is supported via Shapeless HLists:

    case class Point(x: Int, y: Int, z: Int)

    val pointCodec = (int8 :: int8 :: int8).as[Point]

    val encoded: Attempt[BitVector] = pointCodec.encode(Point(-5, 10, 1))
    // Successful(BitVector(24 bits, 0xfb0a01))

    val decoded: Attempt[DecodeResult[Point]] = pointCodec.decode(hex"0xfb0a01".bits)
    // Successful(DecodeResult(Point(-5, 10, 1), BitVector(empty)))

Codecs can also be implicitly resolved, resulting in usage like:

    // Assuming Codec[Point] is in implicit scope

    val encoded: Attempt[BitVector] = Codec.encode(Point(-5, 10, 1))
    // Successful(BitVector(24 bits, 0xfb0a01))

    val decoded: Attempt[DecodeResult[Point]] = Codec.decode[Point](hex"0xfb0a01".bits)
    // Successful(DecodeResult(Point(-5, 10, 1), BitVector(empty)))

Note: by default, scodec provides no implicit codecs. Many sensible defaults can be imported via import scodec.codecs.implicits._, including codecs for various primitive types and some immutable collections.

New codecs can be created by either implementing the Codec trait or by passing an encoder function and decoder function to the Codec apply method. Typically, new codecs are created by applying one or more combinators to existing codecs.

See the guide for detailed documentation. Also, see ScalaDoc. Especially:

Ecosystem

Many libraries have support for scodec:

Examples

There are various examples in the test directory, including codecs for:

The scodec-protocols has production quality codecs for the above examples.

The bmsg library has a codec for the Bitcoin Cash and Bitcoin Core network protocol.

The scodec-msgpack library provides codecs for MessagePack.

The fs2-http project uses FS2, scodec, and shapeless to implement a minimal HTTP client and server.

Testing Your Own Codecs

If you're creating your own Codec instances scodec publishes some of its own test tooling in the scodec-testkit module.

Getting Binaries

See the releases page on the website.

Building

This project uses sbt and requires node.js to be installed in order to run Scala.js tests. To build, run sbt publish-local.

scodec's People

Contributors

mpilquist avatar stew avatar xuwei-k avatar alvaroc1 avatar fristi avatar pchiusano avatar searler avatar jarlakxen avatar danielwegener avatar marq avatar nadavwr avatar jrudnick avatar aoiroaoino avatar knutwalker avatar ogirardot avatar marcsaegesser avatar hairyfotr avatar ajaychandran avatar yzernik avatar pocketberserker avatar pm47 avatar uqix avatar gitter-badger avatar rjwittams avatar maximeburri avatar jvican avatar hkupty avatar kanterov avatar isomarcte avatar ceedubs avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.