|
| 1 | +.. _fundamentals-kotlin-serialization: |
| 2 | + |
| 3 | +==================== |
| 4 | +Kotlin Serialization |
| 5 | +==================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +The Kotlin driver supports the ``kotlinx.serialization`` library for serializing |
| 17 | +and deserializing Kotlin objects. |
| 18 | + |
| 19 | +The driver provides an efficient ``Bson`` serializer that can can be used with |
| 20 | +classes marked as ``@Serializable`` to handle the serialization of Kotlin objects |
| 21 | +to BSON data. |
| 22 | +The ``bson-kotlinx`` library also supports :ref:`custom codecs <kotlin-custom-codec>` |
| 23 | +with configurations to encode defaults, encode nulls, and define class |
| 24 | +discriminators. |
| 25 | + |
| 26 | +.. note:: |
| 27 | + |
| 28 | + To use the ``Codec`` interface instead of the Kotlin serialization library |
| 29 | + to specify custom encoding and decoding of Kotlin objects to BSON data, |
| 30 | + see the :ref:`Codecs <fundamentals-codecs>` documentation. You might choose |
| 31 | + Kotlin serialization if you are already familiar with the framework or |
| 32 | + you prefer to use an idiomatic Kotlin approach. |
| 33 | + |
| 34 | +Although you can use the Kotlin driver with the Kotlin serialization ``Json`` |
| 35 | +library, the ``Json`` serializer does *not* directly support BSON value types such |
| 36 | +as ``ObjectId``. You must provide a custom serializer that can handle the |
| 37 | +conversion between BSON and JSON. |
| 38 | + |
| 39 | +Supported Types |
| 40 | +~~~~~~~~~~~~~~~ |
| 41 | + |
| 42 | +The Kotlin driver supports: |
| 43 | + |
| 44 | +- All Kotlin types that are supported by the Kotlin serialization library |
| 45 | +- All available :manual:`BSON types </reference/bson-types>` |
| 46 | + |
| 47 | +You can implement a custom serializers to handle any other types. See the |
| 48 | +`official Kotlin Documentation <https://kotlinlang.org/docs/serialization.html>`__ |
| 49 | +for more information. |
| 50 | + |
| 51 | +Add Kotlin Serialization to Your Project |
| 52 | +---------------------------------------- |
| 53 | + |
| 54 | +The Kotlin driver serialization support depends on the official Kotlin |
| 55 | +serialization library. You must add `Kotlin Serialization <https://github.com/Kotlin/kotlinx.serialization>`__ |
| 56 | +to your project: |
| 57 | + |
| 58 | +.. tabs:: |
| 59 | + |
| 60 | + .. tab:: |
| 61 | + :tabid: Gradle |
| 62 | + |
| 63 | + If you are using `Gradle <https://gradle.org/>`__ to manage your |
| 64 | + dependencies, add the following to your ``build.gradle.kts`` dependencies list: |
| 65 | + |
| 66 | + .. code-block:: kotlin |
| 67 | + :caption: build.gradle.kts |
| 68 | + |
| 69 | + implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:{+serializationVersion+}") |
| 70 | + |
| 71 | + .. tab:: |
| 72 | + :tabid: Maven |
| 73 | + |
| 74 | + If you are using `Maven <https://maven.apache.org/>`__ to manage your |
| 75 | + dependencies, add the following to your ``pom.xml`` dependencies list: |
| 76 | + |
| 77 | + .. code-block:: kotlin |
| 78 | + :caption: pom.xml |
| 79 | + |
| 80 | + <dependency> |
| 81 | + <groupId>org.jetbrains.kotlinx</groupId> |
| 82 | + <artifactId>kotlinx-serialization-core</artifactId> |
| 83 | + <version>{+serializationVersion+}</version> |
| 84 | + </dependency> |
| 85 | + |
| 86 | +Annotate Data Classes |
| 87 | +--------------------- |
| 88 | + |
| 89 | +To declare a class as serializable, annotate your Kotlin data classes with the |
| 90 | +``@Serializable`` annotation from the Kotlin serialization framework. |
| 91 | + |
| 92 | +You can use your data classes in your code as normal after you mark them as serializable. |
| 93 | +The Kotlin driver and the Kotlin serialization framework will handle the |
| 94 | +BSON serialization and deserialization. |
| 95 | + |
| 96 | +This example shows a simple data class annotated with the following: |
| 97 | + |
| 98 | +- ``@Serializable`` to mark the class as serializable. |
| 99 | +- ``@SerialName`` to specify the name of the ``id`` and ``manufacturer`` properties |
| 100 | + in the BSON document. This can be used in place of the ``@BsonId`` and |
| 101 | + ``@BsonProperty`` annotations, which are unsupported in serializable classes. |
| 102 | +- ``@Contextual`` to mark the BSON ``id`` property to use the built-in ``ObjectIdSerializer``. |
| 103 | + This annotation is required for BSON types to be serialized correctly. |
| 104 | + |
| 105 | +.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.basic-serialization.kt |
| 106 | + :language: kotlin |
| 107 | + |
| 108 | +.. note:: |
| 109 | + |
| 110 | + You cannot use :ref:`annotations <fundamentals-data-class-annotations>` |
| 111 | + from the ``org.bson.codecs.pojo.annotations`` package on ``@Serializable`` data classes. |
| 112 | + |
| 113 | +For more information on serializable classes and available annotation classes, |
| 114 | +see the `official Kotlin Serialization <https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#serializable-classes>`__ |
| 115 | +documentation. |
| 116 | + |
| 117 | +.. _kotlin-custom-codec: |
| 118 | + |
| 119 | +Customize the Serializer Configuration |
| 120 | +-------------------------------------- |
| 121 | + |
| 122 | +You can use the ``KotlinSerializerCodec`` class from the ``org.bson.codecs.kotlinx`` |
| 123 | +package to create a codec for your ``@Serializable`` data classes and |
| 124 | +customize what is stored. |
| 125 | + |
| 126 | +Use the ``BsonConfiguration`` class to define the configuration, |
| 127 | +including whether to encode defaults, encode nulls, or define class discriminators. |
| 128 | + |
| 129 | +To create a custom codec, install the ``bson-kotlinx`` dependency to your project. |
| 130 | + |
| 131 | +.. tabs:: |
| 132 | + |
| 133 | + .. tab:: |
| 134 | + :tabid: Gradle |
| 135 | + |
| 136 | + If you are using `Gradle <https://gradle.org/>`__ to manage your |
| 137 | + dependencies, add the following to your ``build.gradle.kts`` dependencies list: |
| 138 | + |
| 139 | + .. code-block:: kotlin |
| 140 | + :caption: build.gradle.kts |
| 141 | + |
| 142 | + implementation("org.mongodb:bson-kotlinx:{+full-version+}") |
| 143 | + |
| 144 | + .. tab:: |
| 145 | + :tabid: Maven |
| 146 | + |
| 147 | + If you are using `Maven <https://maven.apache.org/>`__ to manage your |
| 148 | + dependencies, add the following to your ``pom.xml`` dependencies list: |
| 149 | + |
| 150 | + .. code-block:: kotlin |
| 151 | + :caption: pom.xml |
| 152 | + |
| 153 | + <dependency> |
| 154 | + <groupId>org.jetbrains.kotlinx</groupId> |
| 155 | + <artifactId>bson-kotlinx</artifactId> |
| 156 | + <version>{+full-version+}</version> |
| 157 | + </dependency> |
| 158 | + |
| 159 | +Then, you can define your codec using the `KotlinSerializerCodec.create() <apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/create.html>`__ |
| 160 | +method and add it to the registry. |
| 161 | + |
| 162 | +The following example shows how to create a codec using the |
| 163 | +``KotlinSerializerCodec.create()`` method and configure it to not encode defaults: |
| 164 | + |
| 165 | +.. code-block:: kotlin |
| 166 | + :copyable: true |
| 167 | + |
| 168 | + import org.bson.codecs.configuration.CodecRegistries |
| 169 | + import org.bson.codecs.kotlinx.BsonConfiguration |
| 170 | + import org.bson.codecs.kotlinx.KotlinSerializerCodec |
| 171 | + |
| 172 | +.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.custom-serialization.kt |
| 173 | + :language: kotlin |
| 174 | + |
| 175 | +For more information about the methods and classes mentioned in this section, |
| 176 | +see the following API Documentation: |
| 177 | + |
| 178 | +- `KotlinSerializerCodec <{+api-kotlin+}/apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/index.html>`__ |
| 179 | +- `KotlinSerializerCodec.create() <{+api-kotlin+}/apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/create.html>`__ |
| 180 | +- `BsonConfiguration <{+api-kotlin+}apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__ |
| 181 | + |
0 commit comments