Skip to main content
Version: 2.x

Bson Codecs

Introductionโ€‹

BSON (Binary JSON) is a binary serialization format used to store and exchange data efficiently. In this article, we will explore how to derive BSON codecs from a ZIO Schema. The zio-schema-bson module, provides support for deriving codecs from ZIO Schema, and makes it easy to communicate data in BSON format.

Installationโ€‹

To use BSON codecs, you need to add the following dependency to your Scala project:

libraryDependencies += "dev.zio" %% "zio-schema-bson" % 1.7.5

BsonSchemaCodecโ€‹

The BsonSchemaCodec object inside the zio.schema.codec package provides the bsonCodec operator which allows us to derive Protobuf codecs from a ZIO Schema:

object BsonSchemaCodec {
def bsonCodec[A](schema: Schema[A]): BsonCodec[A]
}

Exampleโ€‹

Let's see an example of how to derive a BSON codec for a case class using ZIO Schema:

import org.bson.BsonValue
import zio._
import zio.bson._
import zio.schema.codec._
import zio.schema.{DeriveSchema, Schema}

case class Person(name: String, age: Int)

object Person {
implicit val schema: Schema[Person] = DeriveSchema.gen
implicit val bsonCodec: BsonCodec[Person] =
BsonSchemaCodec.bsonCodec(Person.schema)
}

object Main extends ZIOAppDefault {
def run = for {
_ <- ZIO.debug("Bson Example:")
person: Person = Person("John", 42)
encoded: BsonValue = person.toBsonValue
_ <- ZIO.debug(s"person object encoded to BsonValue: $encoded")
decoded <- ZIO.fromEither(encoded.as[Person])
_ <- ZIO.debug(s"BsonValue of person object decoded to Person: $decoded")
} yield ()
}

In the example above, we defined a case class Person with fields name and age. We then derived a ZIO Schema for the Person case class using DeriveSchema.gen.

The BsonSchemaCodec.bsonCodec method allowed us to create a BSON codec for the Person case class by passing its corresponding ZIO Schema. Now, we can effortlessly encode Person objects to BSON and decode BSON values back to Person instances.

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–