BSONReader factories
- Companion:
- class
Value members
Concrete methods
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)
- Value parameters:
- read
the safe function to read BSON values as
T
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.option[String] {
case BSONInteger(0) => Some("zero")
case BSONInteger(1) => Some("one")
case _ => None
}
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)
'''EXPERIMENTAL:''' (API may change without notice)
'''EXPERIMENTAL:''' (API may change without notice)
Creates a BSONReader accepting only BSONArray,
and applying the given safe read
function to each element value.
import reactivemongo.api.bson.BSONReader
def foo(elmReader: BSONReader[(String, Int)]): BSONReader[Seq[(String, Int)]] = BSONReader.sequence(elmReader.readTry _)
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
import reactivemongo.api.bson.{ BSONArray, BSONReader }
val reader = BSONReader.tuple2[String, Int]
val arr = BSONArray("Foo", 20)
reader.readTry(arr) // => Success(("Foo", 20))
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
- See also:
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
- See also:
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONDocumentReader that reads the BSONArray elements.
- See also:
Inherited methods
'''EXPERIMENTAL:''' (API may change without notice)
'''EXPERIMENTAL:''' (API may change without notice)
Creates a BSONReader accepting only BSONArray,
and applying the given safe read
function to each element value.
import reactivemongo.api.bson.BSONReader
case class Element(str: String, v: Int)
def elementReader: BSONReader[Element] = ???
val setReader: BSONReader[Set[Element]] =
BSONReader.iterable[Element, Set](elementReader readTry _)
- Inherited from:
- BSONReaderCompat
Givens
Inherited givens
- Inherited from:
- BSONReaderInstances
- Inherited from:
- BSONReaderInstances