BSONWriter factories.
- Companion:
- class
Type members
Inherited types
Value members
Concrete methods
Creates a BSONWriter based on the given write
function.
This function is called within a scala.util.Try.
Creates a BSONWriter based on the given write
function.
This function is called within a scala.util.Try.
import reactivemongo.api.bson.{ BSONWriter, BSONString }
case class Foo(value: String)
val foo: BSONWriter[Foo] = BSONWriter { f => BSONString(f.value) }
Creates a BSONWriter based on the given partial function.
Creates a BSONWriter based on the given partial function.
A exceptions.ValueDoesNotMatchException is returned as Failure
for any value that is not matched by the write
function.
import reactivemongo.api.bson.{ BSONWriter, BSONInteger }
val strCodeToIntWriter = BSONWriter.collect[String] {
case "zero" => BSONInteger(0)
case "one" => BSONInteger(1)
}
strCodeToIntWriter.writeTry("zero") // Success(BSONInteger(0))
strCodeToIntWriter.writeTry("one") // Success(BSONInteger(1))
strCodeToIntWriter.writeTry("3")
// => Failure(ValueDoesNotMatchException(..))
strCodeToIntWriter.writeOpt("4") // None (as failed)
'''EXPERIMENTAL:''' Creates a BSONWriter based on the given
partially safe write
function.
'''EXPERIMENTAL:''' Creates a BSONWriter based on the given
partially safe write
function.
A exceptions.ValueDoesNotMatchException is returned as Failure
for any value that is not matched by the write
function.
import scala.util.Success
import reactivemongo.api.bson.{ BSONWriter, BSONInteger }
val strCodeToIntWriter = BSONWriter.collectFrom[String] {
case "zero" => Success(BSONInteger(0))
case "one" => Success(BSONInteger(1))
}
strCodeToIntWriter.writeTry("zero") // Success(BSONInteger(0))
strCodeToIntWriter.writeTry("one") // Success(BSONInteger(1))
strCodeToIntWriter.writeTry("3")
// => Failure(IllegalArgumentException(..))
strCodeToIntWriter.writeOpt("4") // None (as failed)
Creates a BSONWriter based on the given safe write
function.
Creates a BSONWriter based on the given safe write
function.
import scala.util.{ Failure, Success }
import reactivemongo.api.bson.{ BSONWriter, BSONInteger }
val strCodeToIntWriter = BSONWriter.from[String] {
case "zero" => Success(BSONInteger(0))
case "one" => Success(BSONInteger(1))
case _ => Failure(new IllegalArgumentException())
}
strCodeToIntWriter.writeTry("zero") // Success(BSONInteger(0))
strCodeToIntWriter.writeTry("one") // Success(BSONInteger(1))
strCodeToIntWriter.writeTry("3")
// => Failure(IllegalArgumentException(..))
strCodeToIntWriter.writeOpt("4") // None (as failed)
- Value parameters:
- write
the safe function to write
T
values as BSON
Creates a BSONWriter based on the given write
function.
Creates a BSONWriter based on the given write
function.
import reactivemongo.api.bson.{ BSONWriter, BSONInteger }
val strCodeToIntWriter = BSONWriter.option[String] {
case "zero" => Some(BSONInteger(0))
case "one" => Some(BSONInteger(1))
case _ => None
}
strCodeToIntWriter.writeTry("zero") // Success(BSONInteger(0))
strCodeToIntWriter.writeTry("one") // Success(BSONInteger(1))
strCodeToIntWriter.writeTry("3")
// => Failure(ValueDoesNotMatchException(..))
strCodeToIntWriter.writeOpt("4") // None (as failed)
'''EXPERIMENTAL:''' (API may change without notice)
'''EXPERIMENTAL:''' (API may change without notice)
Creates a BSONWriter accepting only scala.collection.Iterable,
and applying the given safe write
function to each element value.
import reactivemongo.api.bson.BSONWriter
case class Element(str: String, v: Int)
def elementWriter: BSONWriter[Element] = ???
val seqWriter: BSONWriter[Seq[Element]] =
BSONWriter.sequence[Element](elementWriter writeTry _)
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
import reactivemongo.api.bson.BSONWriter
val writer = BSONWriter.tuple2[String, Int]
writer.writeTry("Foo" -> 20)
// => Success: ['Foo', 20]
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.
Inherited methods
'''EXPERIMENTAL:''' (API may change without notice)
'''EXPERIMENTAL:''' (API may change without notice)
Creates a BSONWriter accepting only scala.collection.Iterable,
and applying the given safe write
function to each element value.
import reactivemongo.api.bson.BSONWriter
case class Element(str: String, v: Int)
def elementWriter: BSONWriter[Element] = ???
val setWriter: BSONWriter[Set[Element]] =
BSONWriter.iterable[Element, Set](elementWriter writeTry _)
- Inherited from:
- BSONWriterCompat (hidden)
Givens
Inherited givens
- Inherited from:
- BSONWriterInstances (hidden)
- Inherited from:
- BSONWriterInstances (hidden)
- Inherited from:
- BSONWriterInstances (hidden)