Skip to content

Commit

Permalink
Added new BsonSerializationOptionsAttribute which serves as the base …
Browse files Browse the repository at this point in the history
…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).
  • Loading branch information
rstam committed Oct 30, 2010
1 parent 9c25039 commit 118aaf1
Show file tree
Hide file tree
Showing 13 changed files with 996 additions and 65 deletions.
2 changes: 2 additions & 0 deletions Bson/Bson.csproj
Expand Up @@ -73,7 +73,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BsonExtensionMethods.cs" />
<Compile Include="DefaultSerializer\Attributes\BsonDateTimeOptionsAttribute.cs" />
<Compile Include="DefaultSerializer\Attributes\BsonRepresentationAttribute.cs" />
<Compile Include="DefaultSerializer\Attributes\BsonSerializationOptionsAttribute.cs" />
<Compile Include="DefaultSerializer\BsonIdGenerators.cs" />
<Compile Include="DefaultSerializer\Conventions\ConventionProfile.cs" />
<Compile Include="DefaultSerializer\Conventions\BsonIdGeneratorConventions.cs" />
Expand Down
58 changes: 58 additions & 0 deletions Bson/DefaultSerializer/Attributes/BsonDateTimeOptionsAttribute.cs
@@ -0,0 +1,58 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MongoDB.Bson.DefaultSerializer {
// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class BsonDateTimeOptionsAttribute : BsonSerializationOptionsAttribute {
#region private fields
private bool dateOnly = false;
private DateTimeKind kind = DateTimeKind.Utc;
private BsonType representation = BsonType.DateTime;
#endregion

#region constructors
public BsonDateTimeOptionsAttribute() {
}
#endregion

#region public properties
public bool DateOnly {
get { return dateOnly; }
set { dateOnly = value; }
}

public DateTimeKind Kind {
get { return kind; }
set { kind = value; }
}

public BsonType Representation {
get { return representation; }
set { representation = value; }
}
#endregion

#region public methods
public override object GetOptions() {
return new DateTimeSerializationOptions { DateOnly = dateOnly, Kind = kind, Representation = representation };
}
#endregion
}
}
6 changes: 6 additions & 0 deletions Bson/DefaultSerializer/Attributes/BsonIdAttribute.cs
Expand Up @@ -23,6 +23,7 @@ namespace MongoDB.Bson.DefaultSerializer {
public class BsonIdAttribute : Attribute {
#region private fields
private Type idGenerator;
private int order = int.MaxValue;
#endregion

#region constructors
Expand All @@ -35,6 +36,11 @@ public class BsonIdAttribute : Attribute {
get { return idGenerator; }
set { idGenerator = value; }
}

public int Order {
get { return order; }
set { order = value; }
}
#endregion
}
}
10 changes: 8 additions & 2 deletions Bson/DefaultSerializer/Attributes/BsonRepresentationAttribute.cs
Expand Up @@ -19,8 +19,8 @@
using System.Text;

namespace MongoDB.Bson.DefaultSerializer {
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class BsonRepresentationAttribute : Attribute {
// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class BsonRepresentationAttribute : BsonSerializationOptionsAttribute {
#region private fields
private BsonType representation;
#endregion
Expand All @@ -38,5 +38,11 @@ BsonType representation
get { return representation; }
}
#endregion

#region public methods
public override object GetOptions() {
return representation;
}
#endregion
}
}
@@ -0,0 +1,33 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MongoDB.Bson.DefaultSerializer {
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public abstract class BsonSerializationOptionsAttribute : Attribute {
#region constructors
protected BsonSerializationOptionsAttribute() {
}
#endregion

#region public methods
public abstract object GetOptions();
#endregion
}
}
93 changes: 45 additions & 48 deletions Bson/DefaultSerializer/BsonClassMap.cs
Expand Up @@ -462,62 +462,59 @@ BsonMemberMap memberMap
private BsonMemberMap AutoMapMember(
MemberInfo memberInfo
) {
var elementName = conventions.ElementNameConvention.GetElementName(memberInfo);
var order = int.MaxValue;
IBsonIdGenerator idGenerator = null;

var idAttribute = (BsonIdAttribute) memberInfo.GetCustomAttributes(typeof(BsonIdAttribute), false).FirstOrDefault();
if (idAttribute != null) {
elementName = "_id"; // if BsonIdAttribute is present ignore BsonElementAttribute
var idGeneratorType = idAttribute.IdGenerator;
if (idGeneratorType != null) {
idGenerator = (IBsonIdGenerator) Activator.CreateInstance(idGeneratorType);
var memberMap = MapMember(memberInfo);

memberMap.SetElementName(conventions.ElementNameConvention.GetElementName(memberInfo));
memberMap.SetIgnoreIfNull(conventions.IgnoreIfNullConvention.IgnoreIfNull(memberInfo));
memberMap.SetSerializeDefaultValue(conventions.SerializeDefaultValueConvention.SerializeDefaultValue(memberInfo));

var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberInfo);
if (defaultValue != null) {
memberMap.SetDefaultValue(defaultValue);
}

foreach (var attribute in memberInfo.GetCustomAttributes(false)) {
var defaultValueAttribute = attribute as BsonDefaultValueAttribute;
if (defaultValueAttribute != null) {
memberMap.SetDefaultValue(defaultValueAttribute.DefaultValue);
memberMap.SetSerializeDefaultValue(defaultValueAttribute.SerializeDefaultValue);
}
} else {
var elementAttribute = (BsonElementAttribute) memberInfo.GetCustomAttributes(typeof(BsonElementAttribute), false).FirstOrDefault();

var elementAttribute = attribute as BsonElementAttribute;
if (elementAttribute != null) {
elementName = elementAttribute.ElementName;
order = elementAttribute.Order;
memberMap.SetElementName(elementAttribute.ElementName);
memberMap.SetOrder(elementAttribute.Order);
continue;
}
}

var memberMap = MapMember(memberInfo);
memberMap.SetElementName(elementName);
if (order != int.MaxValue) {
memberMap.SetOrder(order);
}
if (idAttribute != null) {
memberMap.SetIdGenerator(idGenerator);
SetIdMember(memberMap);
}

var defaultValueAttribute = (BsonDefaultValueAttribute) memberInfo.GetCustomAttributes(typeof(BsonDefaultValueAttribute), false).FirstOrDefault();
if (defaultValueAttribute != null) {
memberMap.SetDefaultValue(defaultValueAttribute.DefaultValue);
memberMap.SetSerializeDefaultValue(defaultValueAttribute.SerializeDefaultValue);
} else {
var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberMap.MemberInfo);
if (defaultValue != null) {
memberMap.SetDefaultValue(defaultValue);
var idAttribute = attribute as BsonIdAttribute;
if (idAttribute != null) {
memberMap.SetElementName("_id");
memberMap.SetOrder(idAttribute.Order);
var idGeneratorType = idAttribute.IdGenerator;
if (idGeneratorType != null) {
var idGenerator = (IBsonIdGenerator) Activator.CreateInstance(idGeneratorType);
memberMap.SetIdGenerator(idGenerator);
}
SetIdMember(memberMap);
continue;
}
memberMap.SetSerializeDefaultValue(conventions.SerializeDefaultValueConvention.SerializeDefaultValue(memberMap.MemberInfo));
}

var ignoreIfNullAttribute = (BsonIgnoreIfNullAttribute) memberInfo.GetCustomAttributes(typeof(BsonIgnoreIfNullAttribute), false).FirstOrDefault();
if (ignoreIfNullAttribute != null) {
memberMap.SetIgnoreIfNull(true);
} else {
memberMap.SetIgnoreIfNull(conventions.IgnoreIfNullConvention.IgnoreIfNull(memberMap.MemberInfo));
}
var ignoreIfNullAttribute = attribute as BsonIgnoreIfNullAttribute;
if (ignoreIfNullAttribute != null) {
memberMap.SetIgnoreIfNull(true);
}

var requiredAttribute = (BsonRequiredAttribute) memberInfo.GetCustomAttributes(typeof(BsonRequiredAttribute), false).FirstOrDefault();
if (requiredAttribute != null) {
memberMap.SetIsRequired(true);
}
var requiredAttribute = attribute as BsonRequiredAttribute;
if (requiredAttribute != null) {
memberMap.SetIsRequired(true);
}

var representationAttribute = (BsonRepresentationAttribute) memberInfo.GetCustomAttributes(typeof(BsonRepresentationAttribute), false).FirstOrDefault();
if (representationAttribute != null) {
memberMap.SetSerializationOptions(representationAttribute.Representation);
// note: this handles subclasses of BsonSerializationOptionsAttribute also
var serializationOptionsAttribute = attribute as BsonSerializationOptionsAttribute;
if (serializationOptionsAttribute != null) {
memberMap.SetSerializationOptions(serializationOptionsAttribute.GetOptions());
}
}

return memberMap;
Expand Down

0 comments on commit 118aaf1

Please sign in to comment.