Skip to content

Commit 118aaf1

Browse files
author
rstam
committed
Added new BsonSerializationOptionsAttribute which serves as the base class for the existing BsonRepresentationAttribute and the new BsonDateTimeOptionsAttribute. Added DateTimeSerializationOptions to allow control over how DateTime values are serialized (representation, local/utc, dateOnly). Refactored AutoMapMember to process attributes in a loop. WriteDateTime in BsonWriter now checks that the value is in UTC (it is the job of the serializer to perform any needed conversions).
1 parent 9c25039 commit 118aaf1

13 files changed

+996
-65
lines changed

Bson/Bson.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
</ItemGroup>
7474
<ItemGroup>
7575
<Compile Include="BsonExtensionMethods.cs" />
76+
<Compile Include="DefaultSerializer\Attributes\BsonDateTimeOptionsAttribute.cs" />
7677
<Compile Include="DefaultSerializer\Attributes\BsonRepresentationAttribute.cs" />
78+
<Compile Include="DefaultSerializer\Attributes\BsonSerializationOptionsAttribute.cs" />
7779
<Compile Include="DefaultSerializer\BsonIdGenerators.cs" />
7880
<Compile Include="DefaultSerializer\Conventions\ConventionProfile.cs" />
7981
<Compile Include="DefaultSerializer\Conventions\BsonIdGeneratorConventions.cs" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright 2010 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
21+
namespace MongoDB.Bson.DefaultSerializer {
22+
// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
23+
public class BsonDateTimeOptionsAttribute : BsonSerializationOptionsAttribute {
24+
#region private fields
25+
private bool dateOnly = false;
26+
private DateTimeKind kind = DateTimeKind.Utc;
27+
private BsonType representation = BsonType.DateTime;
28+
#endregion
29+
30+
#region constructors
31+
public BsonDateTimeOptionsAttribute() {
32+
}
33+
#endregion
34+
35+
#region public properties
36+
public bool DateOnly {
37+
get { return dateOnly; }
38+
set { dateOnly = value; }
39+
}
40+
41+
public DateTimeKind Kind {
42+
get { return kind; }
43+
set { kind = value; }
44+
}
45+
46+
public BsonType Representation {
47+
get { return representation; }
48+
set { representation = value; }
49+
}
50+
#endregion
51+
52+
#region public methods
53+
public override object GetOptions() {
54+
return new DateTimeSerializationOptions { DateOnly = dateOnly, Kind = kind, Representation = representation };
55+
}
56+
#endregion
57+
}
58+
}

Bson/DefaultSerializer/Attributes/BsonIdAttribute.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace MongoDB.Bson.DefaultSerializer {
2323
public class BsonIdAttribute : Attribute {
2424
#region private fields
2525
private Type idGenerator;
26+
private int order = int.MaxValue;
2627
#endregion
2728

2829
#region constructors
@@ -35,6 +36,11 @@ public Type IdGenerator {
3536
get { return idGenerator; }
3637
set { idGenerator = value; }
3738
}
39+
40+
public int Order {
41+
get { return order; }
42+
set { order = value; }
43+
}
3844
#endregion
3945
}
4046
}

Bson/DefaultSerializer/Attributes/BsonRepresentationAttribute.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
using System.Text;
2020

2121
namespace MongoDB.Bson.DefaultSerializer {
22-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
23-
public class BsonRepresentationAttribute : Attribute {
22+
// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
23+
public class BsonRepresentationAttribute : BsonSerializationOptionsAttribute {
2424
#region private fields
2525
private BsonType representation;
2626
#endregion
@@ -38,5 +38,11 @@ public BsonType Representation {
3838
get { return representation; }
3939
}
4040
#endregion
41+
42+
#region public methods
43+
public override object GetOptions() {
44+
return representation;
45+
}
46+
#endregion
4147
}
4248
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright 2010 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
21+
namespace MongoDB.Bson.DefaultSerializer {
22+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
23+
public abstract class BsonSerializationOptionsAttribute : Attribute {
24+
#region constructors
25+
protected BsonSerializationOptionsAttribute() {
26+
}
27+
#endregion
28+
29+
#region public methods
30+
public abstract object GetOptions();
31+
#endregion
32+
}
33+
}

Bson/DefaultSerializer/BsonClassMap.cs

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -462,62 +462,59 @@ private void AutoMapMembers() {
462462
private BsonMemberMap AutoMapMember(
463463
MemberInfo memberInfo
464464
) {
465-
var elementName = conventions.ElementNameConvention.GetElementName(memberInfo);
466-
var order = int.MaxValue;
467-
IBsonIdGenerator idGenerator = null;
468-
469-
var idAttribute = (BsonIdAttribute) memberInfo.GetCustomAttributes(typeof(BsonIdAttribute), false).FirstOrDefault();
470-
if (idAttribute != null) {
471-
elementName = "_id"; // if BsonIdAttribute is present ignore BsonElementAttribute
472-
var idGeneratorType = idAttribute.IdGenerator;
473-
if (idGeneratorType != null) {
474-
idGenerator = (IBsonIdGenerator) Activator.CreateInstance(idGeneratorType);
465+
var memberMap = MapMember(memberInfo);
466+
467+
memberMap.SetElementName(conventions.ElementNameConvention.GetElementName(memberInfo));
468+
memberMap.SetIgnoreIfNull(conventions.IgnoreIfNullConvention.IgnoreIfNull(memberInfo));
469+
memberMap.SetSerializeDefaultValue(conventions.SerializeDefaultValueConvention.SerializeDefaultValue(memberInfo));
470+
471+
var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberInfo);
472+
if (defaultValue != null) {
473+
memberMap.SetDefaultValue(defaultValue);
474+
}
475+
476+
foreach (var attribute in memberInfo.GetCustomAttributes(false)) {
477+
var defaultValueAttribute = attribute as BsonDefaultValueAttribute;
478+
if (defaultValueAttribute != null) {
479+
memberMap.SetDefaultValue(defaultValueAttribute.DefaultValue);
480+
memberMap.SetSerializeDefaultValue(defaultValueAttribute.SerializeDefaultValue);
475481
}
476-
} else {
477-
var elementAttribute = (BsonElementAttribute) memberInfo.GetCustomAttributes(typeof(BsonElementAttribute), false).FirstOrDefault();
482+
483+
var elementAttribute = attribute as BsonElementAttribute;
478484
if (elementAttribute != null) {
479-
elementName = elementAttribute.ElementName;
480-
order = elementAttribute.Order;
485+
memberMap.SetElementName(elementAttribute.ElementName);
486+
memberMap.SetOrder(elementAttribute.Order);
487+
continue;
481488
}
482-
}
483489

484-
var memberMap = MapMember(memberInfo);
485-
memberMap.SetElementName(elementName);
486-
if (order != int.MaxValue) {
487-
memberMap.SetOrder(order);
488-
}
489-
if (idAttribute != null) {
490-
memberMap.SetIdGenerator(idGenerator);
491-
SetIdMember(memberMap);
492-
}
493-
494-
var defaultValueAttribute = (BsonDefaultValueAttribute) memberInfo.GetCustomAttributes(typeof(BsonDefaultValueAttribute), false).FirstOrDefault();
495-
if (defaultValueAttribute != null) {
496-
memberMap.SetDefaultValue(defaultValueAttribute.DefaultValue);
497-
memberMap.SetSerializeDefaultValue(defaultValueAttribute.SerializeDefaultValue);
498-
} else {
499-
var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberMap.MemberInfo);
500-
if (defaultValue != null) {
501-
memberMap.SetDefaultValue(defaultValue);
490+
var idAttribute = attribute as BsonIdAttribute;
491+
if (idAttribute != null) {
492+
memberMap.SetElementName("_id");
493+
memberMap.SetOrder(idAttribute.Order);
494+
var idGeneratorType = idAttribute.IdGenerator;
495+
if (idGeneratorType != null) {
496+
var idGenerator = (IBsonIdGenerator) Activator.CreateInstance(idGeneratorType);
497+
memberMap.SetIdGenerator(idGenerator);
498+
}
499+
SetIdMember(memberMap);
500+
continue;
502501
}
503-
memberMap.SetSerializeDefaultValue(conventions.SerializeDefaultValueConvention.SerializeDefaultValue(memberMap.MemberInfo));
504-
}
505502

506-
var ignoreIfNullAttribute = (BsonIgnoreIfNullAttribute) memberInfo.GetCustomAttributes(typeof(BsonIgnoreIfNullAttribute), false).FirstOrDefault();
507-
if (ignoreIfNullAttribute != null) {
508-
memberMap.SetIgnoreIfNull(true);
509-
} else {
510-
memberMap.SetIgnoreIfNull(conventions.IgnoreIfNullConvention.IgnoreIfNull(memberMap.MemberInfo));
511-
}
503+
var ignoreIfNullAttribute = attribute as BsonIgnoreIfNullAttribute;
504+
if (ignoreIfNullAttribute != null) {
505+
memberMap.SetIgnoreIfNull(true);
506+
}
512507

513-
var requiredAttribute = (BsonRequiredAttribute) memberInfo.GetCustomAttributes(typeof(BsonRequiredAttribute), false).FirstOrDefault();
514-
if (requiredAttribute != null) {
515-
memberMap.SetIsRequired(true);
516-
}
508+
var requiredAttribute = attribute as BsonRequiredAttribute;
509+
if (requiredAttribute != null) {
510+
memberMap.SetIsRequired(true);
511+
}
517512

518-
var representationAttribute = (BsonRepresentationAttribute) memberInfo.GetCustomAttributes(typeof(BsonRepresentationAttribute), false).FirstOrDefault();
519-
if (representationAttribute != null) {
520-
memberMap.SetSerializationOptions(representationAttribute.Representation);
513+
// note: this handles subclasses of BsonSerializationOptionsAttribute also
514+
var serializationOptionsAttribute = attribute as BsonSerializationOptionsAttribute;
515+
if (serializationOptionsAttribute != null) {
516+
memberMap.SetSerializationOptions(serializationOptionsAttribute.GetOptions());
517+
}
521518
}
522519

523520
return memberMap;

0 commit comments

Comments
 (0)