Permalink
Browse files

CSHARP-476: Added support for calling constructors, factory methods o…

…r arbitrary code to instantiate an object during deserialization.
  • Loading branch information...
rstam
rstam committed Jan 16, 2013
1 parent a5d7325 commit e0f958edea7be79a4831b3cf10e990b3e3149d04
@@ -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" />
@@ -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" />
@@ -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" />
@@ -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" />
@@ -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" />
@@ -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);
+ }
+ }
+ }
+}
@@ -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);
+ }
+ }
+ }
+}
@@ -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);
+ }
+}
Oops, something went wrong.

0 comments on commit e0f958e

Please sign in to comment.