diff --git a/bson-official/README.md b/bson-official/README.md index 8dd54de4..33db449b 100644 --- a/bson-official/README.md +++ b/bson-official/README.md @@ -7,3 +7,5 @@ Kotlin-first BSON library, based on the official MongoDB implementations. BSON API for Kotlin, implemented on top of the [official `org.mongodb:bson` library](https://javadoc.io/doc/org.mongodb/bson/latest/index.html). + +The method [`JvmBsonFactory`][opensavvy.ktmongo.bson.official.JvmBsonFactory] is the entry point to all types in the module. It allows creating a new BSON factory using the same configuration as the official library (via the `CodecRegistry`). diff --git a/bson-official/src/jvmMain/kotlin/BsonContext.jvm.kt b/bson-official/src/jvmMain/kotlin/BsonContext.jvm.kt index 2339371b..478b888f 100644 --- a/bson-official/src/jvmMain/kotlin/BsonContext.jvm.kt +++ b/bson-official/src/jvmMain/kotlin/BsonContext.jvm.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025, OpenSavvy and contributors. + * Copyright (c) 2024-2026, OpenSavvy and contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,6 +41,13 @@ import kotlin.time.ExperimentalTime */ interface JvmBsonFactory : BsonFactory { + /** + * The codec registry used by this factory to create new documents. + * + * This registry includes codecs for KtMongo-specific types. + * When mixing KtMongo with the official driver, it is recommended to configure the official driver + * using this registry to ensure types are serialized identically by both libraries. + */ @LowLevelApi val codecRegistry: CodecRegistry @@ -139,6 +146,17 @@ private class JvmBsonFactoryImpl( } } +/** + * Instantiates a new [BsonFactory] that respects the configuration in the specified [codecRegistry]. + * + * Objects created by this factory internally use the official BSON Java library. + * + * Note that the [JvmBsonFactory.codecRegistry] attribute may be different from the passed [codecRegistry]. + * In particular, this function adds new codecs for KtMongo-specific types. + * If mixing KtMongo with the official driver, we recommend configuring the official driver with the + * [JvmBsonFactory.codecRegistry] generated by this function. + * Otherwise, the official driver may serialize KtMongo types differently. + */ fun JvmBsonFactory(codecRegistry: CodecRegistry): JvmBsonFactory = JvmBsonFactoryImpl(codecRegistry)