BSONDocumentHandler

reactivemongo.api.bson.BSONDocumentHandler
See theBSONDocumentHandler companion object

Reads and writers T values to/from BSONDocument.

Attributes

Companion:
object
Graph
Supertypes
trait BSONHandler[T]
trait BSONWriter[T]
trait BSONReader[T]
class Object
trait Matchable
class Any
Known subtypes
object Handler.type

Members list

Concise view

Value members

Concrete methods

Attributes

Definition Classes

Attributes

Definition Classes

Attributes

Definition Classes

Attributes

Definition Classes
final override def narrow[U <: T]: BSONDocumentHandler[U]

Narrows this writer for a compatible type U.

Narrows this writer for a compatible type U.

import reactivemongo.api.bson.BSONWriter

val listWriter: BSONWriter[Seq[String]] =
 implicitly[BSONWriter[Seq[String]]]

val narrowAsListWriter: BSONWriter[List[String]] =
 listWriter.narrow[List[String]]
 // as List[String] <: Seq[String]

Attributes

U

must be a sub-type of T

Definition Classes
final override def widen[U >: T]: BSONDocumentHandler[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]

Attributes

U

must be a super-type of T

Definition Classes

Inherited methods

final override def afterRead[U](f: T => U): BSONDocumentReader[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)
}

Attributes

f

the function to apply

Definition Classes
Inherited from:
BSONDocumentReader

Prepares a BSON writer that returns the result of applying f on the BSON value from this writer.

Prepares a BSON writer that returns the result of applying f on the BSON value from this writer.

If the f function is not defined for a BSONValue, it will results in a Failure.

Attributes

f

the partial function to apply

Definition Classes
Inherited from:
BSONHandler
final override def afterWriteTry(f: BSONValue => Try[BSONValue]): BSONHandler[T]

Prepares a BSON writer that returns the result of applying f on the BSON value from this writer.

Prepares a BSON writer that returns the result of applying f on the BSON value from this writer.

Attributes

f

the safe function to apply

Definition Classes
Inherited from:
BSONHandler
final def as[R](to: T => R, from: R => T): BSONHandler[R]

Attributes

Inherited from:
BSONHandler

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)

Attributes

Definition Classes
Inherited from:
BSONHandler
final override def beforeReadTry(f: BSONValue => Try[BSONValue]): BSONHandler[T]

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.

Attributes

Definition Classes
Inherited from:
BSONHandler
final override def beforeWrite[U](f: U => T): BSONDocumentWriter[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.

Attributes

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)
Definition Classes
Inherited from:
BSONDocumentWriter

Tries to produce an instance of T from the document.

Tries to produce an instance of T from the document.

import scala.util.Try
import reactivemongo.api.bson.{ BSONDocument, BSONDocumentReader }

def fromBSON[T](document: BSONDocument)(
 implicit r: BSONDocumentReader[T]): Try[T] = r.readTry(document)

Attributes

Inherited from:
BSONDocumentReader
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)

Attributes

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)

Attributes

Inherited from:
BSONReader
final 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)

Attributes

Inherited from:
BSONDocumentReader
override def writeOpt(t: T): Option[BSONDocument]

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)

Attributes

Definition Classes
Inherited from:
BSONDocumentWriter
override def writeTry(t: T): Try[BSONDocument]

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)

Attributes

Definition Classes
Inherited from:
BSONDocumentWriter