Skip to content

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

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

fromBson

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

toDocument

scala

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

scala
package dev.mongocamp.driver.mongodb.bson

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

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

  override def toBson(value: Any): BsonValue =
    value match {
      case dt: DateTime =>
        BsonDateTime(dt.toDate)
      case dt: org.joda.time.Duration =>
        BsonString(s"${dt.getMillis}ms")
      case _ =>
        BsonNull()

    }
}

Released under the Apache License 2.0.