BSONWriter

object BSONWriter

BSONWriter factories.

Companion:
class
class Object
trait Matchable
class Any

Type members

Inherited types

type SafeWriter[T] = BSONWriter[T] & SafeBSONWriter[T]
Inherited from:
BSONWriterInstances

Value members

Concrete methods

def apply[T](write: T => BSONValue): BSONWriter[T]

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)
def from[T](write: T => Try[BSONValue]): BSONWriter[T]

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

def option[T](write: T => Option[BSONValue]): BSONWriter[T]

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)
def sequence[T](write: T => Try[BSONValue]): BSONWriter[Seq[T]]

'''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 _)
def tuple2[A : BSONWriter, B : BSONWriter]: BSONWriter[(A, B)]

'''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]
def tuple3[A : BSONWriter, B : BSONWriter, C : BSONWriter]: BSONWriter[(A, B, C)]

'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.

'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.

def tuple4[A : BSONWriter, B : BSONWriter, C : BSONWriter, D : BSONWriter]: BSONWriter[(A, B, C, D)]

'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.

'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.

def tuple5[A : BSONWriter, B : BSONWriter, C : BSONWriter, D : BSONWriter, E : BSONWriter]: BSONWriter[(A, B, C, D, E)]

'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.

'''EXPERIMENTAL:''' Creates a BSONWriter that creates tuple elements as BSONArray elements.

Inherited methods

def iterable[T, M[_]](write: T => Try[BSONValue])(implicit it: M[T] <:< Iterable[T]): BSONWriter[M[T]]

'''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

Givens

Inherited givens

given binaryWriter: SafeWriter[Array[Byte]]
Inherited from:
BSONWriterInstances
given booleanWriter: SafeWriter[Boolean]
Inherited from:
BSONWriterInstances
given bsonMapKeyWriter[K, V <: BSONValue](using KeyWriter[K]): BSONDocumentWriter[Map[K, V]]
Inherited from:
BSONWriterInstances
Inherited from:
BSONWriterInstances
Inherited from:
BSONWriterInstancesLowPrio
given dateTimeWriter: SafeWriter[Instant]
Inherited from:
BSONWriterInstances
Inherited from:
BSONWriterInstances
given doubleWriter: SafeWriter[Double]
Inherited from:
BSONWriterInstances
given floatWriter: SafeWriter[Float]
Inherited from:
BSONWriterInstances
given intWriter: SafeWriter[Int]
Inherited from:
BSONWriterInstances
given localDateTimeWriter: BSONWriter[LocalDateTime]
Inherited from:
BSONWriterInstances
given localDateWriter: BSONWriter[LocalDate]
Inherited from:
BSONWriterInstances
given localTimeWriter: SafeWriter[LocalTime]
Inherited from:
BSONWriterInstances
given localeWriter: SafeWriter[Locale]
Inherited from:
BSONWriterInstances
given longWriter: SafeWriter[Long]
Inherited from:
BSONWriterInstances
given mapKeySafeWriter[K, V](using KeyWriter[K] & SafeKeyWriter[K], BSONWriter[V] & SafeBSONWriter[V]): BSONDocumentWriter[Map[K, V]]
Inherited from:
BSONWriterInstances
given mapKeyWriter[K, V](using KeyWriter[K], BSONWriter[V]): BSONDocumentWriter[Map[K, V]]
Inherited from:
BSONWriterInstances
given mapSafeWriter[V](using BSONWriter[V] & SafeBSONWriter[V]): BSONDocumentWriter[Map[String, V]]
Inherited from:
BSONWriterInstances
Inherited from:
BSONWriterInstances
given offsetDateTimeWriter: BSONWriter[OffsetDateTime]
Inherited from:
BSONWriterInstances
given stringWriter: SafeWriter[String]
Inherited from:
BSONWriterInstances
given tuple2Writer[A : BSONWriter, B : BSONWriter]: BSONWriter[(A, B)]
Inherited from:
BSONWriterInstances
given tuple3Writer[A : BSONWriter, B : BSONWriter, C : BSONWriter]: BSONWriter[(A, B, C)]
Inherited from:
BSONWriterInstances
given tuple4Writer[A : BSONWriter, B : BSONWriter, C : BSONWriter, D : BSONWriter]: BSONWriter[(A, B, C, D)]
Inherited from:
BSONWriterInstances
given tuple5Writer[A : BSONWriter, B : BSONWriter, C : BSONWriter, D : BSONWriter, E : BSONWriter]: BSONWriter[(A, B, C, D, E)]
Inherited from:
BSONWriterInstances
given uriWriter: SafeWriter[URI]
Inherited from:
BSONWriterInstances
given urlWriter: SafeWriter[URL]
Inherited from:
BSONWriterInstances
given uuidWriter: SafeWriter[UUID]
Inherited from:
BSONWriterInstances
given zonedDateTimeWriter: BSONWriter[ZonedDateTime]
Inherited from:
BSONWriterInstances

Implicits

Inherited implicits

final implicit def bsonArrayWriter: BSONWriter[BSONArray]
Exported from package.bsonArrayWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonBinaryWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonBooleanWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonDateTimeWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonDecimalWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonDocumentWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonDoubleWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonIntegerWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonJavaScriptWSWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonJavaScriptWriter
Inherited from:
BSONWriterInstances
final implicit def bsonLongWriter: BSONWriter[BSONLong]
Exported from package.bsonLongWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonMaxKeyWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonMinKeyWriter
Inherited from:
BSONWriterInstances
final implicit def bsonNullWriter: BSONWriter[BSONNull]
Exported from package.bsonNullWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonObjectIDWriter
Inherited from:
BSONWriterInstances
final implicit def bsonRegexWriter: BSONWriter[BSONRegex]
Exported from package.bsonRegexWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonStringWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonSymbolWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonTimestampWriter
Inherited from:
BSONWriterInstances
Exported from package.bsonUndefinedWriter
Inherited from:
BSONWriterInstances