BsonFactory
class BsonFactory(serializersModule: SerializersModule = EmptySerializersModule()) : BsonFactory(source)
Entrypoint for creating BsonDocument and BsonArray instances.
Navigating BSON types
This interface is part of the BSON trinity:
BsonDocument represents an entire BSON document.
BsonArray represents an array of BSON values.
BsonValue represents a single value in isolation.
Serialization
This interface encapsulates methods to serialize and serialize BSON documents with KotlinX.Serialization.
Usage
To create the following BSON document:
{
"name": "Bob",
"isAlive": true,
"children": [
{
"name": "Alice"
},
{
"name": "Charles"
}
]
}Content copied to clipboard
use the code:
val document = factory.buildDocument {
writeString("name", "Alice")
writeBoolean("isAlive", true)
writeArray("children") {
writeDocument {
writeString("name", "Alice")
}
writeDocument {
writeString("name", "Charles")
}
}
}Content copied to clipboard
The value can then be read into Kotlin types:
@Serializable
data class User(
val name: String,
val isAlive: Boolean,
val children: List<Child>,
)
@Serializable
data class Child(
val name: String,
)
val user = document.decode<User>()
println(user.children[0].name) // AliceContent copied to clipboard
Properties
Link copied to clipboard
The SerializersModule used by this factory when encoding or decoding BSON types.