BsonFactory

interface BsonFactory(source)

Entrypoint for creating BsonDocument and BsonArray instances.

Navigating BSON types

This interface is part of the BSON trinity:

Serialization

This interface encapsulates the configuration of the serialization library in use.

For example, if you use the official java or Kotlin MongoDB drivers, all BSON values built by this factory will respect the configured CodecRegistry.

Usage

To create the following BSON document:

{
"name": "Bob",
"isAlive": true,
"children": [
{
"name": "Alice"
},
{
"name": "Charles"
}
]
}

use the code:

val document = factory.buildDocument {
writeString("name", "Alice")
writeBoolean("isAlive", true)
writeArray("children") {
writeDocument {
writeString("name", "Alice")
}
writeDocument {
writeString("name", "Charles")
}
}
}

The value can then be read into Kotlin types:

data class User(
val name: String,
val isAlive: Boolean,
val children: List<Child>,
)

data class Child(
val name: String,
)

val user = document.decode<User>()

println(user.children[0].name) // Alice

Functions

Link copied to clipboard

Instantiates a new BSON array.

Instantiates a new BSON array representing the provided instance.

Link copied to clipboard

Instantiates a new BSON document.

Instantiates a new BSON document representing the provided instance.

Link copied to clipboard
abstract fun <T : Any> encode(obj: T, type: KType): BsonDocument

Writes an arbitrary Kotlin obj into a top-level BSON document.

Link copied to clipboard
inline fun <T : Any> BsonFactory.encode(obj: T): BsonDocument

Writes an arbitrary Kotlin obj into a top-level BSON document.

Link copied to clipboard

Instantiates a new BSON array by reading its bytes representation.

open fun readArray(array: BsonArray): BsonArray

Returns a BsonArray that is tied to this factory, that represents the same data as array.

Link copied to clipboard

Instantiates a new BSON document by reading its bytes representation.

Returns a BsonDocument that is tied to this factory, that represents the same data as document.

Link copied to clipboard
open fun readValue(value: BsonValue): BsonValue

Returns a BsonValue that is tied to this factory, that represents the same data as value.