Skip to content

Commit 64727cd

Browse files
committed
DOCSP-45943: dateonly serialization
1 parent f3f8127 commit 64727cd

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

Diff for: source/fundamentals/serialization.txt

+15-4
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,26 @@ Serializer Registry
5454
The serializer registry contains all registered serializers that are available
5555
to your application. Many of the built-in serializers are automatically
5656
registered to the serializer registry during startup of your application.
57-
However, before you can use a custom serializer, you must add it to the
57+
58+
Register a Serializer
59+
~~~~~~~~~~~~~~~~~~~~~
60+
61+
Before you can use a custom serializer, you must add it to the
5862
serializer registry, as shown in the following example:
5963

6064
.. code-block:: csharp
6165

62-
BsonSerializer.RegisterSerializer(new CustomTypeSerializer());
66+
BsonSerializer.RegisterSerializer(new CustomTypeSerializer());
67+
68+
After you register the serializer, the driver uses it to serialize any
69+
values that are mapped by the serializer.
70+
71+
Access a Serializer from the Registry
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6373

64-
To access the serializer registry, use the ``SerializerRegistry`` property
65-
of the ``BsonSerializer`` class as follows:
74+
To access a specific serializer from the registry, use the
75+
``SerializerRegistry`` property of the ``BsonSerializer`` class as
76+
follows:
6677

6778
.. code-block:: csharp
6879

Diff for: source/fundamentals/serialization/poco.txt

+53-3
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,6 @@ The previous code example sets the following serialization behavior:
542542
- Because ``1900`` is the default value for this property, the driver will ignore the
543543
property if it has this value.
544544

545-
546-
547545
Customize DateTime Serialization
548546
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
549547

@@ -610,6 +608,57 @@ registering the class map:
610608
The ``DateTimeKind`` enum is part of the {+framework+}. For more information on
611609
its members, see the `Microsoft documentation for the DateTimeKind enum. <https://learn.microsoft.com/en-us/dotnet/api/system.datetimekind?view=net-8.0>`__
612610

611+
.. _csharp-poco-dateonly-attr:
612+
613+
Custom DateOnly Serialization
614+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
615+
616+
To customize how the {+driver-short+} serializes `DateOnly
617+
<https://learn.microsoft.com/en-us/dotnet/api/system.dateonly?view=net-8.0>`__
618+
properties, use the ``[BsonDateOnlyOptions()]`` attribute and specify
619+
the following settings:
620+
621+
- ``Representation``: Set to a ``BsonType`` instance that specifies how
622+
the ``DateOnly`` value is stored in MongoDB.
623+
624+
- ``DocumentFormat``: Set to one of the following
625+
``DateOnlyDocumentFormat`` enum values:
626+
627+
- ``DateTimeTicks`` (default): Document contains ``DateTime``
628+
(``BsonType.DateTime``) and ``Ticks`` (``BsonType.Int64``) fields
629+
630+
- ``DateTimeTicks``: Document contains ``Year``, ``Month``, and
631+
``Day`` fields, which have ``BsonType.Int32`` values
632+
633+
In the following code example, the ``PatientRecord`` class contains a
634+
``DateOfBirth`` property that has a ``DateOnly`` value. The
635+
attribute directs the driver to store the value as a nested document
636+
that contains fields for the given year, month, and day.
637+
638+
.. code-block:: csharp
639+
:copyable: true
640+
:emphasize-lines: 5
641+
642+
public class PatientRecord
643+
{
644+
public Guid Id { get; set; }
645+
646+
[BsonDateOnlyOptions(BsonType.Document, DateOnlyDocumentFormat.YearMonthDay)]
647+
public DateOnly DateOfBirth { get; set; }
648+
}
649+
650+
.. tip:: Use the DateOnlySerializer to Set Global Behavior
651+
652+
Instead of using the ``[BsonDateOnlyOptions()]`` attribute at the
653+
property level, you can register a ``DateOnlySerializer`` object to
654+
apply serialization behavior globally:
655+
656+
.. code-block:: csharp
657+
658+
BsonSerializer.RegisterSerializer(
659+
new DateOnlySerializer(BsonType.Document, DateOnlyDocumentFormat.YearMonthDay)
660+
);
661+
613662
Customize Dictionary Serialization
614663
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
615664

@@ -752,4 +801,5 @@ guide, see the following API documentation:
752801
- `[BsonDateTimeOptions()] <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Attributes.BsonDateTimeOptionsAttribute.html>`__
753802
- `[BsonDictionaryOptions()] <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Attributes.BsonDictionaryOptionsAttribute.html>`__
754803
- `ConventionPack <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Conventions.ConventionPack.html>`__
755-
- `InsertOne() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.InsertMany.html>`__
804+
- `InsertOne()
805+
<{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.InsertMany.html>`__

Diff for: source/whats-new.txt

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ The 3.2 driver release includes the following new features:
122122
To learn more about Atlas Vector Search with the {+driver-short+}, see the
123123
:ref:`csharp-atlas-vector-search` guide.
124124

125+
- Adds the ``DocumentFormat`` property to the ``DateOnlySerializer`` to
126+
allow you to customize the way that the driver stores ``DateOnly``
127+
values. This release also adds the ``[BsonDateOnlyOptions()]``
128+
attribute to customize serialization behavior at the property level.
129+
To learn more, see the :ref:`csharp-poco-dateonly-attr` section of the
130+
Serialization guide.
131+
125132
.. _csharp-version-3.1:
126133

127134
What's New in 3.1

0 commit comments

Comments
 (0)