pbson is a BSON library for Scala.
The goal of this library is to create at compile-time the boilerplate necessary to encode and decode of a certain type.
The pbson provides generic codec derivation using Shapeless.
This library provides another way encode and decode case classes for
mongo scala driver.
Decoder instead of throw Exception return Either[BsonError, T]
pbson can derive bson encoder and decoder:
BsonEncoder[T] : T => BsonValue
BsonDecoder[T] : BsonValue => Either[BsonError, T]
import pbson._
import pbson.semiauto._
case class MyId(value: String) extends AnyVal
case class TestCase(a: Int, b: Option[String], id: MyId)
implicit val testCaseEncoder: BsonEncoder[TestCase] = deriveEncoder
implicit val testCaseDecoder: BsonDecoder[TestCase] = deriveDecoder
val test = TestCase(3, Some("45"), MyId("000"))
val bson = test.toBson
println(bson)
// { "a" : 3, "b" : "45", "id" : "000" }
println(bson.fromBson[TestCase]())
// Right(TestCase(3,Some(45),MyId(000)))
The current stable version is 0.0.18
If you're using SBT, add the following line to your build file:
resolvers += "JCenter" at "https://jcenter.bintray.com/"
libraryDependencies += "ru.twistedlogic" %% "pbson" % "0.0.20"