⚠️ Archived in favor of a future project (agcom/kbson)⚠️
Bson serialization format implementation for Kotlinx serialization, based on The BSON library (org.mongodb.bson).
JVM only.
Also, provides useful tools to integrate with MongoDB Java driver. For example, the out of box SerializationCodecRegistry.
Currently, only supports Kotlinx serialization runtime 0.20.0.
*.gradle:repositories { jcenter() // Make sure jcenter is added to your project repositories } dependencies { implementation 'com.github.agcom.bson:bson-serialization:0.5.0'// The bson serialization library implementation 'com.github.agcom.bson:bson-mongodb:0.5.0'// MongoDB driver extensions }*.gradle.kts: Same as*.gradle, with some small tweaks.
Here is a small example,
importkotlinx.serialization.*importcom.github.agcom.bson.serialization.*importorg.bson.*
@Serializable
data classProject(valname:String, vallanguage:String)
val bson =Bson()
funmain() {
val data =Project("com.github.agcom.bson", "Kotlin")
// Serializingval bsonValue = bson.toBson(Project.serializer(), data) // A `BsonValue` child, in this case a `BsonDocument`println(bsonValue) // {"name": "com.github.agcom.bson", "language": "Kotlin"}// Deserializingprintln(
bson.fromBson(Project.serializer(), bsonValue)
) // Project(name=com.github.agcom.bson, language=Kotlin)
}The following functions can be found in the com.github.agcom.bson.serialization.Bson class.
toBsonandfromBson: The above example ☝️.dumpandload:importkotlinx.serialization.*importcom.github.agcom.bson.serialization.*importorg.bson.* @Serializable data classProject(valname:String, vallanguage:String) val bson =Bson() funmain() { val data =Project("com.github.agcom.bson", "Kotlin") // Dumpval bytes = bson.dump(Project.serializer(), data) // Loadprintln( bson.load(Project.serializer(), bytes) ) // Project(name=com.github.agcom.bson, language=Kotlin) }
Doesn't support loading/dumping primitive types.
Various bson types adapter serializers can be found under com.github.agcom.bson.serialization.serializers package.
Those are all registered as default contextual serializers, so you can use @ContextualSerializer safely.
For example,
BsonValueSerializer,TemporalSerializerandRegexSerializer.
Provides extensions to integrate with MongoDB Java driver.
Serialization codec
An adapter between
KSerializerandCodec.importkotlinx.serialization.*importcom.github.agcom.bson.serialization.*importorg.bson.codecs.Codecimportcom.github.agcom.bson.mongodb.codecs.* @Serializable data classProject(valname:String, vallanguage:String) val bson =Bson() funmain() { val codec:Codec<Project> serializer=SerializationCodec(bson, Project.serializer()) // Look here... }
Serialization codec registry
An adapter between
BsonandCodecRegistry.importkotlinx.serialization.*importcom.github.agcom.bson.serialization.*importcom.github.agcom.bson.mongodb.codecs.*importorg.bson.codecs.configuration.*importcom.mongodb.MongoClientSettings @Serializable data classProject(valname:String, vallanguage:String) val bson =Bson() funmain() { // Composing two registriesval registry:CodecRegistry=CodecRegistries.fromRegistries( MongoClientSettings.getDefaultCodecRegistry(), // The driver's default codec registrySerializationCodecRegistry(bson) // Serialization registry ) ... }
It's recommended to compose the serialization registry after the default registry. This reduces hip-hops (better performance) when working with simple bson types.