Skip to content

Commit

Permalink
DOCSP-28748: bson-kotlin note
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Jan 22, 2024
1 parent 742f87f commit cf65791
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions source/fundamentals/data-formats/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
Kotlin Serialization
====================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: code example, data model, conversion

.. contents:: On this page
:local:
:backlinks: none
Expand All @@ -13,23 +20,26 @@ Kotlin Serialization
Overview
--------

The Kotlin driver supports the ``kotlinx.serialization`` library for serializing
and deserializing Kotlin objects.
The Kotlin driver supports the ``kotlinx.serialization`` library for
serializing and deserializing Kotlin objects.

The driver provides an efficient ``Bson`` serializer that can can be used with
The driver provides an efficient ``Bson`` serializer that you can use with
classes marked as ``@Serializable`` to handle the serialization of Kotlin objects
to BSON data.
The ``bson-kotlinx`` library also supports :ref:`custom codecs <kotlin-custom-codec>`
with configurations to encode defaults, encode nulls, and define class
discriminators.
to BSON data.

You can also install the ``bson-kotlinx`` library to support
:ref:`custom codecs <kotlin-custom-codec>` with configurations to encode
defaults, encode nulls, and define class discriminators.

.. note::

To use the ``Codec`` interface instead of the Kotlin serialization library
to specify custom encoding and decoding of Kotlin objects to BSON data,
see the :ref:`Codecs <fundamentals-codecs>` documentation. You might choose
Kotlin serialization if you are already familiar with the framework or
you prefer to use an idiomatic Kotlin approach.
To learn about how to use the ``Codec`` interface instead of the
Kotlin serialization library to specify custom encoding and decoding
of Kotlin objects to BSON data, see the :ref:`Codecs
<fundamentals-codecs>` guide.

You might choose Kotlin serialization if you are already familiar
with the framework or if you prefer to use an idiomatic Kotlin approach.

Although you can use the Kotlin driver with the Kotlin serialization ``Json``
library, the ``Json`` serializer does *not* directly support BSON value types such
Expand Down Expand Up @@ -162,6 +172,18 @@ To create a custom codec, install the ``bson-kotlinx`` dependency to your projec
<version>{+full-version+}</version>
</dependency>

.. note:: bson-kotlin Dependency

You can also optionally install the ``bson-kotlin`` dependency
through the default codec registry. This dependency uses reflection
and the codec registry to support Kotlin data classes, but it does
not support certain POJO annotations such as ``BsonDiscriminator``,
``BsonExtraElements``, and ``BsonConstructor``. To learn more, see
the `bson-kotlin API documentation <{+api+}/apidocs/bson-kotlin/index.html>`__

In most cases, we recommend that you install and use the faster

Check failure on line 184 in source/fundamentals/data-formats/serialization.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.Wordiness] Consider using 'usually' instead of 'In most cases'. Raw Output: {"message": "[MongoDB.Wordiness] Consider using 'usually' instead of 'In most cases'.", "location": {"path": "source/fundamentals/data-formats/serialization.txt", "range": {"start": {"line": 184, "column": 4}}}, "severity": "ERROR"}
``bson-kotlinx`` library for codec configuration.

Then, you can define your codec using the
`KotlinSerializerCodec.create()
<{+api+}/apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/index.html>`__
Expand Down

0 comments on commit cf65791

Please sign in to comment.