medeia / medeia

A BSON libary for scala using typeclasses



Build status Maven Central Maintainability Join the chat at Scala Steward badge Cats friendly

medeia is a BSON library for Scala.

Goal / Scope

The goal of medeia is to make bson decoding / encoding as easy and fun as JSON decoding / encoding is with circe or argonaut.

medeia includes decoders and encoders for common data structures as well as automatic derivation of decoders and encoders for case classes using shapeless.


Add to sbt

  libraryDependencies += "de.megaera" %% "medeia" % "0.4.1"

Currently no complete documentation is present, additional examples can be found in the test suites. If you have questions: don't hesitate to ask via github issues.

decoding and encoding example:

  import org.mongodb.scala.bson._

  import medeia.syntax._

  val stringList = List("a", "b")
  val encoded = stringList.toBson
  // BsonArray{values=[BsonString{value='a'}, BsonString{value='b'}]}
  val bsonArray = BsonArray("a", "b")
  val decoded = bsonArray.fromBson[List[String]]
  // Right(List(a,b)

automatic derivation example for case classes and sealed trait hierarchies:

  import org.mongodb.scala.bson._

  import medeia.encoder.BsonEncoder
  import medeia.decoder.BsonDecoder
  import medeia.generic.semiauto._
  import medeia.syntax._

  case class Simple(int: Int, string: Option[String])
  implicit val simpleEncoder: BsonEncoder[Simple] = deriveBsonEncoder
  val encoded = Simple(1, Some("a")).toBson
  // {"string": "a", "int": 1}

  implicit val simpleDecoder: BsonDecoder[Simple] = deriveBsonDecoder
  val doc = BsonDocument("int" -> 1, "string" -> "string")
  val decoded = doc.fromBson[Simple]
  // Right(Simple(1,Some(string)))

  sealed trait Trait
  case class Foo(answer: Int) extends Trait
  case class Bar(bar: String) extends Trait

  implicit val fooCodec: BsonDocumentCodec[Foo] = deriveBsonCodec
  implicit val barCodec: BsonDocumentCodec[Bar] = deriveBsonCodec
  implicit val traitCodec: BsonDocumentCodec[Trait] = deriveBsonCodec

  val encoded = Foo(42).toBson
  // {"answer": 42, "type": "Foo"}

A transformation function for keynames can be provided as follows:

  import medeia.generic.GenericDerivationOptions
  import medeia.encoder.BsonEncoder
  import medeia.generic.semiauto._
  import medeia.syntax._

  case class Simple(fieldInScala: Int)
  implicit val genericDerivationOptions: GenericDerivationOptions[Simple] =
    GenericDerivationOptions { case "fieldInScala" => "fieldInBson" }
  implicit val simpleEncoder: BsonEncoder[Simple] = deriveBsonEncoder
  val encoded = Simple(1).toBson
  // {"fieldInBson": 1}

GenericDerivationOptions works for encoding and decoding. If the provided partial function is not defined for a key no tranformation is used.


A separate module exists for encoding and decoding enumeratum enums:

  libraryDependencies += "de.megaera" %% "medeia-enumeratum" % "0.4.0"
  import medeia.syntax._
  import medeia.enumeratum.Enumeratum

  import enumeratum.Enum
  import enumeratum.EnumEntry

  import scala.collection.immutable

  sealed abstract class TestEnum(override val entryName: String) extends EnumEntry

  object TestEnum extends Enum[TestEnum] {
    override val values: immutable.IndexedSeq[TestEnum] = findValues

    case object A extends TestEnum("A")
    case object B extends TestEnum("B")
    case object C extends TestEnum("C")

    implicit val codec: BsonCodec[TestEnum] = Enumeratum.codec(TestEnum)

  // "A"


medeia is licensed under the Apache License, Version 2.0 (the “License”); you may not use this software except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.