Creates a BSONReader based on the given read
function.
Creates a BSONReader based on the given read
function.
import reactivemongo.api.bson.{ BSONReader, BSONInteger } val intToStrCodeReader = BSONReader[String] { case BSONInteger(0) => "zero" case BSONInteger(1) => "one" case _ => "unknown" } intToStrCodeReader.readTry(BSONInteger(0)) // Success("zero") intToStrCodeReader.readTry(BSONInteger(1)) // Success("one") intToStrCodeReader.readTry(BSONInteger(2)) // Success("unknown")
Any Exception
thrown by the read
function will be caught.
Creates a BSONReader based on the given partial function.
Creates a BSONReader based on the given partial function.
A exceptions.ValueDoesNotMatchException is returned as Failure
for any BSON value that is not matched by the read
function.
import reactivemongo.api.bson.{ BSONReader, BSONInteger } val intToStrCodeReader = BSONReader.collect[String] { case BSONInteger(0) => "zero" case BSONInteger(1) => "one" } intToStrCodeReader.readTry(BSONInteger(0)) // Success("zero") intToStrCodeReader.readTry(BSONInteger(1)) // Success("one") intToStrCodeReader.readTry(BSONInteger(2)) // => Failure(ValueDoesNotMatchException(..)) intToStrCodeReader.readOpt(BSONInteger(3)) // None (as failed)
Creates a BSONReader based on the given read
function.
Creates a BSONReader based on the given read
function.
import scala.util.{ Failure, Success } import reactivemongo.api.bson.{ BSONReader, BSONInteger } val intToStrCodeReader = BSONReader.from[String] { case BSONInteger(0) => Success("zero") case BSONInteger(1) => Success("one") case _ => Failure(new IllegalArgumentException()) } intToStrCodeReader.readTry(BSONInteger(0)) // Success("zero") intToStrCodeReader.readTry(BSONInteger(1)) // Success("one") intToStrCodeReader.readTry(BSONInteger(2)) // => Failure(IllegalArgumentException(..)) intToStrCodeReader.readOpt(BSONInteger(3)) // None (as failed)
the safe function to read BSON values as T
BSONReader factories