Skip to content

Commit 4ce75e7

Browse files
authored
DOCSP-37272: Update and correct serialization-related code examples (#190)
DOCSP-37272: Update and correct serialization-related code examples
1 parent 828ff73 commit 4ce75e7

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

source/fundamentals/serialization.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ The following code example shows a custom ``BsonRegularExpression`` serializer:
8484

8585
.. code-block:: csharp
8686

87-
class CustomRegularExpressionSerializer : SerializerBase<RegularExpression>
87+
class CustomRegularExpressionSerializer : SerializerBase<Regex>
8888
{
89-
public override RegularExpression Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
89+
public override Regex Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
9090
{
9191
var type = context.Reader.GetCurrentBsonType();
9292
switch (type)
@@ -101,7 +101,7 @@ The following code example shows a custom ``BsonRegularExpression`` serializer:
101101
}
102102
}
103103

104-
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, RegularExpression value)
104+
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Regex value)
105105
{
106106
context.Writer.WriteRegularExpression(value);
107107
}

source/fundamentals/serialization/poco.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ serialization-related attributes:
599599
- ``[BsonRepresentation()]``, which specifies serialization of the ``Price`` field as a BSON ``Double`` type
600600
- ``[BsonDefaultValue()]``, which sets the ``Name`` property to
601601
``"Generic item"`` if no value has been assigned to it
602+
- ``[BsonDateTimeOptions(DateOnly = true)]``, which specifies that the ``DateTime`` property
603+
represents only a date value, with no associated time
602604

603605
.. literalinclude:: ../../includes/fundamentals/code-examples/Clothing.cs
604606
:start-after: start-model
@@ -616,7 +618,14 @@ The following code instantiates a ``Clothing`` object and inserts the document i
616618
Name = "Denim Jacket",
617619
InStock = false,
618620
Price = 32.99m,
619-
ColorSelection = new List<string> { "dark wash", "light wash" }
621+
ColorSelection = new List<string> { "dark wash", "light wash" },
622+
ListedDate = DateTime.Parse("Jan 1, 2007"),
623+
SizeGuide = new Dictionary<string, string>()
624+
{
625+
{"Small", "Chest: 38\", Waist: 38\", Shoulders: 15\""},
626+
{"Medium", "Chest: 40\", Waist: 40\", Shoulders: 15.5\""},
627+
{"Large", "Chest: 42\", Waist: 40\", Shoulders: 16\""}
628+
}
620629
};
621630

622631
_myColl.InsertOne(doc);
@@ -631,7 +640,13 @@ The BSON representation of the inserted document looks like this:
631640
"name": "Denim Jacket",
632641
"inStock": false,
633642
"price": 32.99,
634-
"colorSelection": [ "dark wash", "light wash" ]
643+
"colorSelection": [ "dark wash", "light wash" ],
644+
"listedDate" : ISODate("2007-01-01T00:00:00Z"),
645+
"sizeGuide" : {
646+
"Small" : "Chest: 38\", Waist: 38\", Shoulders: 15\"",
647+
"Medium" : "Chest: 40\", Waist: 40\", Shoulders: 15.5\"",
648+
"Large" : "Chest: 42\", Waist: 40\", Shoulders: 16\""
649+
}
635650
}
636651

637652
Additional Information

source/includes/fundamentals/code-examples/Clothing.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ public class Clothing
2121

2222
[BsonElement("colorSelection")]
2323
public List<string> ColorSelection { get; set; }
24+
25+
[BsonElement("listedDate")]
26+
[BsonDateTimeOptions(DateOnly = true)]
27+
public DateTime ListedDate { get; set; }
28+
29+
[BsonElement("sizeGuide")]
30+
public Dictionary<string, string> SizeGuide { get; set; }
2431
}
2532
// end-model

0 commit comments

Comments
 (0)