Skip to content

BsonValueWriter

Generator of BSON values.

This interface is used to write a generic BSON value. For example, the root BSON value, or a specific value of a field.

To write fields in a BSON document, see BsonFieldWriter.

Instances of this interface are commonly obtained by calling the BsonFactory.buildArray function.

Functions

pipe

Writes the arbitrary obj into this writer.

Note that the object will be written as-is, with no safety checks whatsoever. Only use this method if you are absolutely sure attackers cannot control the contents of obj.

If in doubt, prefer using writeSafe.

writeArray

@LowLevelApi
abstract fun writeArray(block: BsonValueWriter.() -> Unit)

writeBinaryData

@LowLevelApi
abstract fun writeBinaryData(type: UByte, data: ByteArray)

writeBoolean

@LowLevelApi
abstract fun writeBoolean(value: Boolean)

writeDateTime

@LowLevelApi
abstract fun writeDateTime(value: Long)

writeDBPointer

@LowLevelApi
abstract fun writeDBPointer(namespace: String, id: ByteArray)

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeDecimal128

@LowLevelApi
abstract fun writeDecimal128(low: Long, high: Long)

writeDocument

@LowLevelApi
abstract fun writeDocument(block: BsonFieldWriter.() -> Unit)

writeDouble

@LowLevelApi
abstract fun writeDouble(value: Double)

writeInstant

@LowLevelApi
open fun writeInstant(value: Instant)

Writes an Instant. Conversion function on top of writeDateTime.

writeInt32

@LowLevelApi
abstract fun writeInt32(value: Int)
@LowLevelApi
open fun writeInt32(value: Short)
@LowLevelApi
open fun writeInt32(value: Byte)

writeInt64

@LowLevelApi
abstract fun writeInt64(value: Long)

writeJavaScript

@LowLevelApi
abstract fun writeJavaScript(code: String)

writeJavaScriptWithScope

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeMaxKey

@LowLevelApi
abstract fun writeMaxKey()

writeMinKey

@LowLevelApi
abstract fun writeMinKey()

writeNull

@LowLevelApi
abstract fun writeNull()

writeObjectId

@LowLevelApi
abstract fun writeObjectId(id: ByteArray)

writeRegularExpression

@LowLevelApi
abstract fun writeRegularExpression(pattern: String, options: String)

writeSafe

@LowLevelApi
abstract fun <T> writeSafe(obj: T, type: KType)

@LowLevelApi
inline fun <T> writeSafe(obj: T)

Writes an arbitrary obj into a BSON document.

All nested values are escaped as necessary such that the result is a completely inert BSON document.

Serialization configuration

This method uses the serialization methods configured in the BsonFactory that created this instance.

For example, if you use the official Java or Kotlin MongoDB drivers, this method will use your configured CodecRegistry.

Example

data class User(
    val _id: ObjectId,
    val profile: Profile,
)

data class Profile(
    val name: String,
    val age: Int?,
)

val factory: BsonFactory = 

val bson = factory.buildDocument {
    write("user") {
        writeSafe("user", User(ObjectId("69c93e17b96e83b72d11b734"), Profile("Bob", 30)))
    }
}

val user = bson.decode<User>()

println(user._id)          // ObjectId(69c93e17b96e83b72d11b734)
println(user.profile.name) // Bob
println(user.profile.age)  // 30

writeString

@LowLevelApi
abstract fun writeString(value: String)

writeSymbol

@LowLevelApi
abstract fun writeSymbol(value: String)

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeTimestamp

@LowLevelApi
abstract fun writeTimestamp(value: Timestamp)

writeUndefined

@LowLevelApi
abstract fun writeUndefined()

Deprecated

This functionality is deprecated in the BSON specification. See https://bsonspec.org/spec.html

writeVector

@LowLevelApi
open fun writeVector(vector: Vector)