Bson

BSON converter

BSON converter is used for bidirectional converting of BSON data.

  • Supports most Scala / Java types.
  • Case class support
  • Java Bean support
  • pluggable

Usage

toBson


val result = BsonConverter.toBson(3) // result = BsonInt32(3)

fromBson


val result = BsonConverter.fromBson(BsonInt32(3)) // result = 3

toDocument


case class Base(int: Int, Long: Long, float: Float, double: Double, string: String, date: Date = new Date()) object Base { def apply(): Base = new Base(1, 2, 3, 4, "test") } val document = Converter.toDocument(Base()) // Document((float,BsonDouble{value=3.0}), (string,BsonString{value='test'}), // (double,BsonDouble{value=4.0}), (Long,BsonInt64{value=2}), (date, // BsonDateTime{value=1531166757627}), (int,BsonInt32{value=1}))

Plugins

Example

sourcepackage dev.mongocamp.driver.mongodb.bson

import org.bson.BsonValue
import org.joda.time.DateTime
import org.mongodb.scala.bson.{BsonDateTime, BsonNull}

class JodaConverterPlugin extends AbstractConverterPlugin {
  override def customClassList: List[Class[_]] = List(classOf[DateTime])

  override def toBson(value: Any): BsonValue =
    value match {
      case dt: DateTime => BsonDateTime(dt.toDate)
      case _ =>
        BsonNull()

    }
}