BSONDocumentHandler factories
Attributes
- Companion:
- trait
- Graph
- Supertypes
- class Objecttrait Matchableclass Any
- Self type
- BSONDocumentHandler.type
Members list
Value members
Concrete methods
Document handler factory.
Document handler factory.
import reactivemongo.api.bson.{ BSONDocument, BSONDocumentHandler }
case class Bar(score: Double)
val h: BSONDocumentHandler[Bar] = BSONDocumentHandler[Bar](
read = { doc =>
Bar(doc.getOrElse[Double]("score", 0D))
},
write = { bar => BSONDocument("score" -> bar.score) })
Attributes
'''EXPERIMENTAL:''' Creates a BSONDocumentHandler
based on the given read
and write
functions.
'''EXPERIMENTAL:''' Creates a BSONDocumentHandler
based on the given read
and write
functions.
import reactivemongo.api.bson.{ BSONDocumentHandler, BSONDocument }
val handler = BSONDocumentHandler.collect[String](
read = {
case doc: BSONDocument => doc.getAsOpt[Int]("value").collect {
case 0 => "zero"
case 1 => "one"
} getOrElse ""
},
write = {
case "zero" => BSONDocument("value" -> 0)
case "one" => BSONDocument("value" -> 1)
})
handler.readTry(BSONDocument("value" -> 0)) // Success("zero")
handler.readOpt(BSONDocument("value" -> 3)) // None (as failed)
handler.writeTry("one") // Success(BSONDocument("value" -> 1))
handler.writeOpt("3") // None (as failed)
Attributes
Creates a BSONDocumentHandler
based on the given safe read
and write
functions.
Creates a BSONDocumentHandler
based on the given safe read
and write
functions.
import scala.util.Success
import reactivemongo.api.bson.{ BSONDocument, BSONDocumentHandler }
case class Bar(score: Double)
val h: BSONDocumentHandler[Bar] = BSONDocumentHandler.from[Bar](
read = _.getAsTry[Double]("score").map(Bar(_)),
write = { bar => Success(BSONDocument("score" -> bar.score)) })
Attributes
Creates a BSONDocumentHandler
based on the given read
and write
functions.
Creates a BSONDocumentHandler
based on the given read
and write
functions.
import reactivemongo.api.bson.{ BSONDocumentHandler, BSONDocument }
val handler = BSONDocumentHandler.option[String](
read = {
case doc: BSONDocument => doc.getAsOpt[Int]("value").collect {
case 0 => "zero"
case 1 => "one"
}
case _ => None
},
write = {
case "zero" => Some(BSONDocument("value" -> 0))
case "one" => Some(BSONDocument("value" -> 1))
case _ => None
})
handler.readTry(BSONDocument("value" -> 0)) // Success("zero")
handler.readOpt(BSONDocument("value" -> 3)) // None (as failed)
handler.writeTry("one") // Success(BSONDocument("value" -> 1))
handler.writeOpt("3") // None (as failed)
Attributes
Returns a document handler for a type T
,
provided there are a writer and a reader for it.
Returns a document handler for a type T
,
provided there are a writer and a reader for it.
import reactivemongo.api.bson.{
BSONDocumentHandler, BSONDocumentReader, BSONDocumentWriter
}
def foo[T](
implicit r: BSONDocumentReader[T],
w: BSONDocumentWriter[T]): BSONDocumentHandler[T] =
BSONDocumentHandler.provided[T]