Skip to content

EvgeneKiiski/pbson

Repository files navigation

Build status Coverage Status

PBson - Pure BSON

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]

Quick Start

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)))

Getting pbson

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"

Resources

docs

examples

Cats

pbson-cats

Scalaz

pbson-scalaz under construction