BSONHandler

trait BSONHandler[T] extends BSONReader[T] with BSONWriter[T]

A BSON handler is able to both read and write T values from/to BSON representation.

import scala.util.Try
import reactivemongo.api.bson.{ BSONHandler, BSONValue }

def roundtrip[T](value: T)(implicit handler: BSONHandler[T]): Try[Boolean] =
 for {
   bson: BSONValue <- handler.writeTry(value)
   dser <- handler.readTry(bson)
 } yield (dser == value) // true
Companion:
object
trait BSONWriter[T]
trait BSONReader[T]
class Object
trait Matchable
class Any
object BSONBinaryHandler.type
object BSONBooleanHandler.type
object BSONDecimalHandler.type
object Handler.type
object BSONDoubleHandler.type
object BSONFloatHandler.type
object BSONIntegerHandler.type
object BSONLocaleHandler.type
object BSONLongHandler.type
object BSONStringHandler.type
object BSONURIHandler.type
object BSONURLHandler.type
object BSONUUIDHandler.type

Value members

Concrete methods

Definition Classes
final override def afterWriteTry(f: BSONValue => Try[BSONValue]): BSONHandler[T]
Definition Classes
final def as[R](to: T => R, from: R => T): BSONHandler[R]
Definition Classes
final override def beforeReadTry(f: BSONValue => Try[BSONValue]): BSONHandler[T]
Definition Classes
@SuppressWarnings(scala.Array.apply[java.lang.String]("AsInstanceOf")(scala.reflect.ClassTag.apply[java.lang.String](classOf[java.lang.String])))
override def narrow[U <: T]: BSONHandler[U]
Definition Classes
@SuppressWarnings(scala.Array.apply[java.lang.String]("AsInstanceOf")(scala.reflect.ClassTag.apply[java.lang.String](classOf[java.lang.String])))
override def widen[U >: T]: BSONHandler[U]
Definition Classes

Inherited methods

def afterRead[U](f: T => U): BSONReader[U]

Prepares a BSONReader that returns the result of applying f on the result of this reader.

Prepares a BSONReader that returns the result of applying f on the result of this reader.

import scala.util.Try
import reactivemongo.api.bson.{ BSONReader, BSONValue }

// Try to return an integer + 1,
// from any T that can be read from BSON
// and is a numeric type
def fromBSON[T](bson: BSONValue)(
 implicit r: BSONReader[T], n: Numeric[T]): Try[Int] = {
 val r2: BSONReader[Int] = r.afterRead { v => n.toInt(v) + 1 }
 r2.readTry(bson)
}
Value parameters:
f

the function to apply

Inherited from:
BSONReader
def beforeWrite[U](f: U => T): BSONWriter[U]

Prepares a BSON writer that converts the input before calling the current writer.

Prepares a BSON writer that converts the input before calling the current writer.

Value parameters:
f

the function apply the U input value to convert at T value used to the current writer

import reactivemongo.api.bson.BSONWriter
val w: BSONWriter[String] =
 implicitly[BSONWriter[Int]].beforeWrite(_.size)
w.writeTry("foo") // Success: BSONInteger(3)
Inherited from:
BSONWriter
def readOpt(bson: BSONValue): Option[T]

Tries to produce an instance of T from the bson value, returns None if an error occurred.

Tries to produce an instance of T from the bson value, returns None if an error occurred.

import reactivemongo.api.bson.{ BSONReader, BSONValue }

def fromBSON[T](bson: BSONValue)(implicit r: BSONReader[T]): Option[T] =
 r.readOpt(bson)
Inherited from:
BSONReader
def readOrElse(bson: BSONValue, default: => T): T

Tries to produce an instance of T from the bson value, returns the default value if an error occurred.

Tries to produce an instance of T from the bson value, returns the default value if an error occurred.

import reactivemongo.api.bson.{ BSONReader, BSONValue }

def fromBSON[T](bson: BSONValue, v: T)(implicit r: BSONReader[T]): T =
 r.readOrElse(bson, v)
Inherited from:
BSONReader
def readTry(bson: BSONValue): Try[T]

Tries to produce an instance of T from the bson value.

Tries to produce an instance of T from the bson value.

import scala.util.Try
import reactivemongo.api.bson.{ BSONReader, BSONValue }

def fromBSON[T](bson: BSONValue)(implicit r: BSONReader[T]): Try[T] =
 r.readTry(bson)
Inherited from:
BSONReader

Tries to produce a BSON value from an instance of T, returns None if an error occurred.

Tries to produce a BSON value from an instance of T, returns None if an error occurred.

import reactivemongo.api.bson.{ BSONWriter, BSONValue }

def maybeBSON[T](value: T)(implicit w: BSONWriter[T]): Option[BSONValue] =
 w.writeOpt(value)
Inherited from:
BSONWriter
def writeTry(t: T): Try[BSONValue]

Tries to produce a BSON value from an instance of T.

Tries to produce a BSON value from an instance of T.

import scala.util.Try
import reactivemongo.api.bson.{ BSONWriter, BSONValue }

def toBSON[T](value: T)(implicit w: BSONWriter[T]): Try[BSONValue] =
 w.writeTry(value)
Inherited from:
BSONWriter