Skip to content

Commit 1ad2875

Browse files
author
rstam
committed
New approach to CSHARP-284 and CSHARP-308. Serialization attributes are just another way of setting serialization options and are now processed by the options themselves in the new ApplyAttribute method. Serialization options now have a few new methods: ApplyAttribute, Clone and Freeze. Removed IsItemsOption and ItemSerializationOptionsWrapper; whether an attribute is for the container or for the items is determined by context. Introduced new BsonBaseSerializationOptions class to make implementing serialization options easier. All serializers that use serialization options now return the default serialization options from the new GetDefaultSerializationOptions method. There are minute changes to the unit tests; that is a good thing as it implies these changes are not breaking changes.
1 parent dd5ce03 commit 1ad2875

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+966
-454
lines changed

Bson/Bson.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
<Compile Include="Serialization\Conventions\ExtraElementsMemberConventions.cs" />
100100
<Compile Include="Serialization\Conventions\IgnoreIfDefaultConventions.cs" />
101101
<Compile Include="Serialization\Conventions\SerializationOptionsConventions.cs" />
102-
<Compile Include="Serialization\Options\ItemSerializationOptionsWrapper.cs" />
102+
<Compile Include="Serialization\Options\ArraySerializationOptions.cs" />
103+
<Compile Include="Serialization\Options\BsonBaseSerializationOptions.cs" />
103104
<Compile Include="Serialization\Options\DateTimeSerializationOptions.cs" />
104105
<Compile Include="Serialization\Options\DictionarySerializationOptions.cs" />
105106
<Compile Include="Serialization\Options\DocumentSerializationOptions.cs" />

Bson/ObjectModel/BsonDocument.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,11 +1007,19 @@ public void RemoveElement(BsonElement element)
10071007
/// <param name="options">The serialization options (can be null).</param>
10081008
public void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
10091009
{
1010-
bsonWriter.WriteStartDocument();
1010+
var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
1011+
if (documentSerializationOptions == null)
1012+
{
1013+
var message = string.Format(
1014+
"Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
1015+
BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
1016+
BsonUtils.GetFriendlyTypeName(options.GetType()));
1017+
throw new BsonSerializationException(message);
1018+
}
10111019

1012-
var documentOptions = (options == null) ? DocumentSerializationOptions.Defaults : (DocumentSerializationOptions)options;
1020+
bsonWriter.WriteStartDocument();
10131021
int idIndex;
1014-
if (documentOptions.SerializeIdFirst && _indexes.TryGetValue("_id", out idIndex))
1022+
if (documentSerializationOptions.SerializeIdFirst && _indexes.TryGetValue("_id", out idIndex))
10151023
{
10161024
_elements[idIndex].WriteTo(bsonWriter);
10171025
}

Bson/Serialization/Attributes/BsonDateTimeOptionsAttribute.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,5 @@ public BsonType Representation
6969
get { return _representation; }
7070
set { _representation = value; }
7171
}
72-
73-
// public methods
74-
/// <summary>
75-
/// Gets the serialization options specified by this attribute.
76-
/// </summary>
77-
/// <returns>The serialization options.</returns>
78-
public override IBsonSerializationOptions GetOptions()
79-
{
80-
IBsonSerializationOptions serializationOptions;
81-
if (_dateOnly)
82-
{
83-
serializationOptions = new DateTimeSerializationOptions(_dateOnly, _representation);
84-
}
85-
else
86-
{
87-
serializationOptions = new DateTimeSerializationOptions(_kind, _representation);
88-
}
89-
return CheckIfIsItemsOptions(serializationOptions);
90-
}
9172
}
9273
}

Bson/Serialization/Attributes/BsonDefaultValueAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2424
/// Specifies the default value for a field or property.
2525
/// </summary>
2626
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
27-
public class BsonDefaultValueAttribute : Attribute
27+
public class BsonDefaultValueAttribute : BsonSerializationOptionsAttribute
2828
{
2929
// private fields
3030
private object _defaultValue;

Bson/Serialization/Attributes/BsonDictionaryOptionsAttribute.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,5 @@ public DictionaryRepresentation Representation
5858
get { return _representation; }
5959
set { _representation = value; }
6060
}
61-
62-
// public methods
63-
/// <summary>
64-
/// Gets the serialization options specified by this attribute.
65-
/// </summary>
66-
/// <returns>The serialization options.</returns>
67-
public override IBsonSerializationOptions GetOptions()
68-
{
69-
var serializationOptions = new DictionarySerializationOptions(_representation);
70-
return CheckIfIsItemsOptions(serializationOptions);
71-
}
7261
}
7362
}

Bson/Serialization/Attributes/BsonDiscriminatorAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2424
/// Specifies the discriminator and related options for a class.
2525
/// </summary>
2626
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
27-
public class BsonDiscriminatorAttribute : Attribute
27+
public class BsonDiscriminatorAttribute : BsonSerializationOptionsAttribute
2828
{
2929
// private fields
3030
private string _discriminator;

Bson/Serialization/Attributes/BsonElementAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2424
/// Specifies the element name and related options for a field or property.
2525
/// </summary>
2626
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
27-
public class BsonElementAttribute : Attribute
27+
public class BsonElementAttribute : BsonSerializationOptionsAttribute
2828
{
2929
// private fields
3030
private string _elementName;

Bson/Serialization/Attributes/BsonExtraElementsAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2424
/// Indicates that extra elements should be ignored when this class is deserialized.
2525
/// </summary>
2626
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
27-
public class BsonExtraElementsAttribute : Attribute
27+
public class BsonExtraElementsAttribute : BsonSerializationOptionsAttribute
2828
{
2929
}
3030
}

Bson/Serialization/Attributes/BsonIdAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2424
/// Specifies that this is the Id field or property.
2525
/// </summary>
2626
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
27-
public class BsonIdAttribute : Attribute
27+
public class BsonIdAttribute : BsonSerializationOptionsAttribute
2828
{
2929
// private fields
3030
private Type _idGenerator;

Bson/Serialization/Attributes/BsonIgnoreAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Bson.Serialization.Attributes
2424
/// Indicates that this field or property should be ignored when this class is serialized.
2525
/// </summary>
2626
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
27-
public class BsonIgnoreAttribute : Attribute
27+
public class BsonIgnoreAttribute : BsonSerializationOptionsAttribute
2828
{
2929
}
3030
}

0 commit comments

Comments
 (0)