Skip to content

Commit

Permalink
DOCSP-37272: Update and correct serialization-related code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmorisi committed May 6, 2024
1 parent 828ff73 commit 419c5d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 3 additions & 3 deletions source/fundamentals/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ The following code example shows a custom ``BsonRegularExpression`` serializer:

.. code-block:: csharp

class CustomRegularExpressionSerializer : SerializerBase<RegularExpression>
class CustomRegularExpressionSerializer : SerializerBase<Regex>
{
public override RegularExpression Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
public override Regex Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var type = context.Reader.GetCurrentBsonType();
switch (type)
Expand All @@ -101,7 +101,7 @@ The following code example shows a custom ``BsonRegularExpression`` serializer:
}
}

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, RegularExpression value)
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Regex value)
{
context.Writer.WriteRegularExpression(value);
}
Expand Down
19 changes: 17 additions & 2 deletions source/fundamentals/serialization/poco.txt
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ serialization-related attributes:
- ``[BsonRepresentation()]``, which specifies serialization of the ``Price`` field as a BSON ``Double`` type
- ``[BsonDefaultValue()]``, which sets the ``Name`` property to
``"Generic item"`` if no value has been assigned to it
- ``[BsonDateTimeOptions(DateOnly = true)]``, which specifies that our DateTime property
only represents a date value with no associated time

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

_myColl.InsertOne(doc);
Expand All @@ -631,7 +640,13 @@ The BSON representation of the inserted document looks like this:
"name": "Denim Jacket",
"inStock": false,
"price": 32.99,
"colorSelection": [ "dark wash", "light wash" ]
"colorSelection": [ "dark wash", "light wash" ],
"listedDate" : ISODate("2007-01-01T00:00:00Z"),
"sizeGuide" : {
"Small" : "Chest: 38\", Waist: 38\", Shoulders: 15\"",
"Medium" : "Chest: 40\", Waist: 40\", Shoulders: 15.5\"",
"Large" : "Chest: 42\", Waist: 40\", Shoulders: 16\""
}
}

Additional Information
Expand Down
7 changes: 7 additions & 0 deletions source/includes/fundamentals/code-examples/Clothing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@ public class Clothing

[BsonElement("colorSelection")]
public List<string> ColorSelection { get; set; }

[BsonElement("listedDate")]
[BsonDateTimeOptions(DateOnly = true)]
public DateTime ListedDate { get; set; }

[BsonElement("sizeGuide")]
public Dictionary<string, string> SizeGuide { get; set; }
}
// end-model

0 comments on commit 419c5d3

Please sign in to comment.