BSONReader

trait BSONReader[T]

A reader that produces an instance of T from a subtype of BSONValue.

Companion:
object
class Object
trait Matchable
class Any
object Handler.type
object Handler.type
trait BSONHandler[T]
object BSONBinaryHandler.type
object BSONBooleanHandler.type
object BSONDecimalHandler.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
object Handler.type

Value members

Abstract methods

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)

Concrete 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

Prepares a BSONReader that transforms the input BSON value, using the given f function, before passing the transformed BSON value to the current reader.

Prepares a BSONReader that transforms the input BSON value, using the given f function, before passing the transformed BSON value to the current reader.

import reactivemongo.api.bson.{
 BSONReader, BSONInteger, BSONNull, BSONString
}

val normalizingReader: BSONReader[Int] =
 implicitly[BSONReader[Int]].beforeRead {
   case BSONNull => BSONInteger(-1)
   case BSONString(s) => BSONInteger(s.size)
   // other values are unchanged
 }

normalizingReader.readOpt(BSONNull) // Some(-1)
normalizingReader.readTry(BSONString("foo")) // Success(3)
normalizingReader.readOpt(BSONInteger(4)) // unchanged: Some(4)

Prepares a BSONReader that transforms the input BSON value, using the given f function, before passing the transformed BSON value to the current reader.

Prepares a BSONReader that transforms the input BSON value, using the given f function, before passing the transformed BSON value to the current reader.

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)
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)
@SuppressWarnings(scala.Array.apply[java.lang.String]("AsInstanceOf")(scala.reflect.ClassTag.apply[java.lang.String](classOf[java.lang.String])))
def widen[U >: T]: BSONReader[U]

Widens this reader for a compatible type U.

Widens this reader for a compatible type U.

import reactivemongo.api.bson.BSONReader

val listReader: BSONReader[List[String]] =
 implicitly[BSONReader[List[String]]]

val widenAsSeqReader: BSONReader[Seq[String]] =
 listReader.widen[Seq[String]]
 // as Seq[String] >: List[String]
Type parameters:
U

must be a super-type of T