hnaderi / named-codec   0.3.0

Apache License 2.0 Website GitHub

Scala3 codec adapter that separates types and payloads

Scala versions: 3.x
Scala.js versions: 1.x
Scala Native versions: 0.5

named-codec

Scala3 codec adapter that separates types and payloads Cats friendly

named-codec-core Scala version support javadoc GitHub Workflow Status GitHub
Scala Steward badge

this is a tiny utility library that provides a simple codec adapter, that helps with creating codec that encode/decode type name separately.

This is mostly helpful in messaging applications, where payload and message types are separated; or for scenarios that you need to store a separate type name to enable type filtering.

Usage

This library is currently available for Scala binary version 3.3 on both JVM, JS and native.

To use the latest version, include the following in your build.sbt:

libraryDependencies ++= Seq(
  "dev.hnaderi" %% "named-codec" % "@VERSION@"
)

// or circe module directly

libraryDependencies ++= Seq(
  "dev.hnaderi" %% "named-codec-circe" % "@VERSION@"
)
enum Data {
  case A
  case B(i: Int)
  case C(s: String, i: Int)
}

import io.circe.generic.auto.*
import dev.hnaderi.namedcodec.*

val codec = CirceAdapter.of[Data]

codec.encode(Data.C("string", 101))

// EncodedMessage(name = "C", data = { "s": "string", i: 101 })