Skip to content

Commit

Permalink
CSHARP-476: Added support for calling constructors, factory methods o…
Browse files Browse the repository at this point in the history
…r arbitrary code to instantiate an object during deserialization.
  • Loading branch information
rstam committed Jan 16, 2013
1 parent a5d7325 commit e0f958e
Show file tree
Hide file tree
Showing 19 changed files with 2,244 additions and 23 deletions.
10 changes: 10 additions & 0 deletions MongoDB.Bson/MongoDB.Bson.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@
<Compile Include="IO\JsonReaderSettings.cs" />
<Compile Include="ObjectModel\ICustomBsonTypeMapper.cs" />
<Compile Include="ObjectModel\BsonTypeMapperOptions.cs" />
<Compile Include="Serialization\Attributes\BsonConstructorAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonDateTimeOptionsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonDictionaryOptionsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonExtraElementsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonFactoryMethodAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonIgnoreIfDefaultAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonMemberMapAttributeUsageAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonNoIdAttribute.cs" />
Expand All @@ -107,10 +109,13 @@
<Compile Include="Serialization\Attributes\IBsonMemberMapAttribute.cs" />
<Compile Include="Serialization\Attributes\IBsonClassMapAttribute.cs" />
<Compile Include="Serialization\Attributes\IBsonPostProcessingAttribute.cs" />
<Compile Include="Serialization\Attributes\IBsonCreatorMapAttribute.cs" />
<Compile Include="Serialization\BsonClassMapSerializationProvider.cs" />
<Compile Include="Serialization\BsonCreatorMap.cs" />
<Compile Include="Serialization\BsonDocumentBackedClass.cs" />
<Compile Include="Serialization\Conventions\AttributeConventionPack.cs" />
<Compile Include="Serialization\Conventions\CamelCaseElementNameConvention.cs" />
<Compile Include="Serialization\Conventions\NamedParameterCreatorMapConvention.cs" />
<Compile Include="Serialization\Conventions\ConventionBase.cs" />
<Compile Include="Serialization\Conventions\ConventionPack.cs" />
<Compile Include="Serialization\Conventions\ConventionRegistry.cs" />
Expand All @@ -121,6 +126,7 @@
<Compile Include="Serialization\Conventions\DelegatePostProcessingConvention.cs" />
<Compile Include="Serialization\Conventions\HierarchicalDiscriminatorConvention.cs" />
<Compile Include="Serialization\Conventions\IClassMapConvention.cs" />
<Compile Include="Serialization\Conventions\ICreatorMapConvention.cs" />
<Compile Include="Serialization\Conventions\IConvention.cs" />
<Compile Include="Serialization\Conventions\IConventionPack.cs" />
<Compile Include="Serialization\Conventions\IgnoreExtraElementsConvention.cs" />
Expand Down Expand Up @@ -153,6 +159,9 @@
<Compile Include="Serialization\Conventions\ScalarDiscriminatorConvention.cs" />
<Compile Include="Serialization\Conventions\StandardDiscriminatorConvention.cs" />
<Compile Include="Serialization\Conventions\StringObjectIdIdGeneratorConvention.cs" />
<Compile Include="Serialization\CreatorMapDelegateCompiler.cs" />
<Compile Include="Serialization\ExpressionVisitor.cs" />
<Compile Include="Serialization\ICreatorSelector.cs" />
<Compile Include="Serialization\IdGenerators\BsonBinaryDataGuidGenerator.cs" />
<Compile Include="Serialization\IdGenerators\BsonObjectIdGenerator.cs" />
<Compile Include="Serialization\IdGenerators\CombGuidGenerator.cs" />
Expand All @@ -161,6 +170,7 @@
<Compile Include="Serialization\IdGenerators\ObjectIdGenerator.cs" />
<Compile Include="Serialization\IdGenerators\StringObjectIdGenerator.cs" />
<Compile Include="Serialization\IdGenerators\ZeroIdChecker.cs" />
<Compile Include="Serialization\MostArgumentsCreatorSelector.cs" />
<Compile Include="Serialization\Serializers\BitArraySerializer.cs" />
<Compile Include="Serialization\Serializers\BitmapSerializer.cs" />
<Compile Include="Serialization\Serializers\BooleanSerializer.cs" />
Expand Down
67 changes: 67 additions & 0 deletions MongoDB.Bson/Serialization/Attributes/BsonConstructorAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright 2010-2013 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;

namespace MongoDB.Bson.Serialization.Attributes
{
/// <summary>
/// Specifies that this constructor should be used for creator-based deserialization.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public class BsonConstructorAttribute : Attribute, IBsonCreatorMapAttribute
{
// private fields
private string[] _argumentNames;

// constructors
/// <summary>
/// Initializes a new instance of the BsonConstructorAttribute class.
/// </summary>
public BsonConstructorAttribute()
{ }

/// <summary>
/// Initializes a new instance of the BsonConstructorAttribute class.
/// </summary>
/// <param name="argumentNames">The names of the members that the creator argument values come from.</param>
public BsonConstructorAttribute(params string[] argumentNames)
{
_argumentNames = argumentNames;
}

// public properties
/// <summary>
/// Gets the names of the members that the creator arguments values come from.
/// </summary>
public string[] ArgumentNames
{
get { return _argumentNames; }
}

// public methods
/// <summary>
/// Applies a modification to the creator map.
/// </summary>
/// <param name="creatorMap">The creator map.</param>
public void Apply(BsonCreatorMap creatorMap)
{
if (_argumentNames != null)
{
creatorMap.SetArguments(_argumentNames);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright 2010-2013 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;

namespace MongoDB.Bson.Serialization.Attributes
{
/// <summary>
/// Specifies that this factory method should be used for creator-based deserialization.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class BsonFactoryMethodAttribute : Attribute, IBsonCreatorMapAttribute
{
// private fields
private string[] _argumentNames;

// constructors
/// <summary>
/// Initializes a new instance of the BsonFactoryMethodAttribute class.
/// </summary>
public BsonFactoryMethodAttribute()
{ }

/// <summary>
/// Initializes a new instance of the BsonFactoryMethodAttribute class.
/// </summary>
/// <param name="argumentNames">The names of the members that the creator argument values come from.</param>
public BsonFactoryMethodAttribute(params string[] argumentNames)
{
_argumentNames = argumentNames;
}

// public properties
/// <summary>
/// Gets the names of the members that the creator arguments values come from.
/// </summary>
public string[] ArgumentNames
{
get { return _argumentNames; }
}

// public methods
/// <summary>
/// Applies a modification to the creator map.
/// </summary>
/// <param name="creatorMap">The creator map.</param>
public void Apply(BsonCreatorMap creatorMap)
{
if (_argumentNames != null)
{
creatorMap.SetArguments(_argumentNames);
}
}
}
}
29 changes: 29 additions & 0 deletions MongoDB.Bson/Serialization/Attributes/IBsonCreatorMapAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright 2010-2013 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.
*/

namespace MongoDB.Bson.Serialization
{
/// <summary>
/// Represents an attribute used to modify a creator map.
/// </summary>
public interface IBsonCreatorMapAttribute
{
/// <summary>
/// Applies the attribute to the creator map.
/// </summary>
/// <param name="creatorMap">The creator map.</param>
void Apply(BsonCreatorMap creatorMap);
}
}
Loading

0 comments on commit e0f958e

Please sign in to comment.