Encoding and Decoding

Encoding a value to a plain Array[Byte]:

CBOR
sourceimport io.bullet.borer.Cbor

val value = List("foo", "bar", "baz") // example value

val bytes: Array[Byte] =
  Cbor.encode(value).toByteArray // throws on error

bytes ==> hex"9f63666f6f636261726362617aff"
JSON
sourceimport io.bullet.borer.Json

val value = List("foo", "bar", "baz") // example value

val bytes: Array[Byte] =
  Json.encode(value).toByteArray // throws on error

// or immediately decode the bytes into a String:

val json: String =
  Json.encode(value).toUtf8String // throws on error

json ==> """["foo","bar","baz"]"""
bytes ==> json.getBytes("UTF8")

Decoding a plain Array[Byte] back to a certain type:

CBOR
sourceimport io.bullet.borer.Cbor

val list: List[String] =
  Cbor.decode(bytes).to[List[String]].value // throws on error
JSON
sourceimport io.bullet.borer.Json

val list: List[String] =
  Json.decode(bytes).to[List[String]].value // throws on error

If you don’t want BORER to throw exceptions you can use the following variants to give you a Try instead:

CBOR
sourceimport io.bullet.borer.Cbor

val encoded: Try[Array[Byte]] =
  Cbor.encode(value).toByteArrayTry

val decoded: Try[List[String]] =
  Cbor.decode(bytes).to[List[String]].valueTry
JSON
sourceimport io.bullet.borer.Json

val encoded: Try[Array[Byte]] =
  Json.encode(value).toByteArrayTry

val decoded: Try[List[String]] =
  Json.decode(bytes).to[List[String]].valueTry

Or, if you prefer encoding/decoding to an Either instance:

CBOR
sourceimport io.bullet.borer.*

val encoded: Either[Borer.Error[Output], Array[Byte]] =
  Cbor.encode(value).to[Array[Byte]].resultEither

val decoded: Either[Borer.Error[Input.Position], List[String]] =
  Cbor.decode(bytes).to[List[String]].valueEither
JSON
sourceimport io.bullet.borer.*

val encoded: Either[Borer.Error[Output], Array[Byte]] =
  Json.encode(value).to[Array[Byte]].resultEither

val decoded: Either[Borer.Error[Input.Position], List[String]] =
  Json.decode(bytes).to[List[String]].valueEither

Additionally borer offers even more top-level API calls that give you the respective Output or Input instances either directly or wrapped in Try or Either.
Check the sources of the central borer API entry point for more info.