Skip to content

Commit e0f958e

Browse files
author
rstam
committed
CSHARP-476: Added support for calling constructors, factory methods or arbitrary code to instantiate an object during deserialization.
1 parent a5d7325 commit e0f958e

19 files changed

+2244
-23
lines changed

MongoDB.Bson/MongoDB.Bson.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@
9494
<Compile Include="IO\JsonReaderSettings.cs" />
9595
<Compile Include="ObjectModel\ICustomBsonTypeMapper.cs" />
9696
<Compile Include="ObjectModel\BsonTypeMapperOptions.cs" />
97+
<Compile Include="Serialization\Attributes\BsonConstructorAttribute.cs" />
9798
<Compile Include="Serialization\Attributes\BsonDateTimeOptionsAttribute.cs" />
9899
<Compile Include="Serialization\Attributes\BsonDictionaryOptionsAttribute.cs" />
99100
<Compile Include="Serialization\Attributes\BsonExtraElementsAttribute.cs" />
101+
<Compile Include="Serialization\Attributes\BsonFactoryMethodAttribute.cs" />
100102
<Compile Include="Serialization\Attributes\BsonIgnoreIfDefaultAttribute.cs" />
101103
<Compile Include="Serialization\Attributes\BsonMemberMapAttributeUsageAttribute.cs" />
102104
<Compile Include="Serialization\Attributes\BsonNoIdAttribute.cs" />
@@ -107,10 +109,13 @@
107109
<Compile Include="Serialization\Attributes\IBsonMemberMapAttribute.cs" />
108110
<Compile Include="Serialization\Attributes\IBsonClassMapAttribute.cs" />
109111
<Compile Include="Serialization\Attributes\IBsonPostProcessingAttribute.cs" />
112+
<Compile Include="Serialization\Attributes\IBsonCreatorMapAttribute.cs" />
110113
<Compile Include="Serialization\BsonClassMapSerializationProvider.cs" />
114+
<Compile Include="Serialization\BsonCreatorMap.cs" />
111115
<Compile Include="Serialization\BsonDocumentBackedClass.cs" />
112116
<Compile Include="Serialization\Conventions\AttributeConventionPack.cs" />
113117
<Compile Include="Serialization\Conventions\CamelCaseElementNameConvention.cs" />
118+
<Compile Include="Serialization\Conventions\NamedParameterCreatorMapConvention.cs" />
114119
<Compile Include="Serialization\Conventions\ConventionBase.cs" />
115120
<Compile Include="Serialization\Conventions\ConventionPack.cs" />
116121
<Compile Include="Serialization\Conventions\ConventionRegistry.cs" />
@@ -121,6 +126,7 @@
121126
<Compile Include="Serialization\Conventions\DelegatePostProcessingConvention.cs" />
122127
<Compile Include="Serialization\Conventions\HierarchicalDiscriminatorConvention.cs" />
123128
<Compile Include="Serialization\Conventions\IClassMapConvention.cs" />
129+
<Compile Include="Serialization\Conventions\ICreatorMapConvention.cs" />
124130
<Compile Include="Serialization\Conventions\IConvention.cs" />
125131
<Compile Include="Serialization\Conventions\IConventionPack.cs" />
126132
<Compile Include="Serialization\Conventions\IgnoreExtraElementsConvention.cs" />
@@ -153,6 +159,9 @@
153159
<Compile Include="Serialization\Conventions\ScalarDiscriminatorConvention.cs" />
154160
<Compile Include="Serialization\Conventions\StandardDiscriminatorConvention.cs" />
155161
<Compile Include="Serialization\Conventions\StringObjectIdIdGeneratorConvention.cs" />
162+
<Compile Include="Serialization\CreatorMapDelegateCompiler.cs" />
163+
<Compile Include="Serialization\ExpressionVisitor.cs" />
164+
<Compile Include="Serialization\ICreatorSelector.cs" />
156165
<Compile Include="Serialization\IdGenerators\BsonBinaryDataGuidGenerator.cs" />
157166
<Compile Include="Serialization\IdGenerators\BsonObjectIdGenerator.cs" />
158167
<Compile Include="Serialization\IdGenerators\CombGuidGenerator.cs" />
@@ -161,6 +170,7 @@
161170
<Compile Include="Serialization\IdGenerators\ObjectIdGenerator.cs" />
162171
<Compile Include="Serialization\IdGenerators\StringObjectIdGenerator.cs" />
163172
<Compile Include="Serialization\IdGenerators\ZeroIdChecker.cs" />
173+
<Compile Include="Serialization\MostArgumentsCreatorSelector.cs" />
164174
<Compile Include="Serialization\Serializers\BitArraySerializer.cs" />
165175
<Compile Include="Serialization\Serializers\BitmapSerializer.cs" />
166176
<Compile Include="Serialization\Serializers\BooleanSerializer.cs" />
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright 2010-2013 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+
18+
namespace MongoDB.Bson.Serialization.Attributes
19+
{
20+
/// <summary>
21+
/// Specifies that this constructor should be used for creator-based deserialization.
22+
/// </summary>
23+
[AttributeUsage(AttributeTargets.Constructor)]
24+
public class BsonConstructorAttribute : Attribute, IBsonCreatorMapAttribute
25+
{
26+
// private fields
27+
private string[] _argumentNames;
28+
29+
// constructors
30+
/// <summary>
31+
/// Initializes a new instance of the BsonConstructorAttribute class.
32+
/// </summary>
33+
public BsonConstructorAttribute()
34+
{ }
35+
36+
/// <summary>
37+
/// Initializes a new instance of the BsonConstructorAttribute class.
38+
/// </summary>
39+
/// <param name="argumentNames">The names of the members that the creator argument values come from.</param>
40+
public BsonConstructorAttribute(params string[] argumentNames)
41+
{
42+
_argumentNames = argumentNames;
43+
}
44+
45+
// public properties
46+
/// <summary>
47+
/// Gets the names of the members that the creator arguments values come from.
48+
/// </summary>
49+
public string[] ArgumentNames
50+
{
51+
get { return _argumentNames; }
52+
}
53+
54+
// public methods
55+
/// <summary>
56+
/// Applies a modification to the creator map.
57+
/// </summary>
58+
/// <param name="creatorMap">The creator map.</param>
59+
public void Apply(BsonCreatorMap creatorMap)
60+
{
61+
if (_argumentNames != null)
62+
{
63+
creatorMap.SetArguments(_argumentNames);
64+
}
65+
}
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright 2010-2013 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+
18+
namespace MongoDB.Bson.Serialization.Attributes
19+
{
20+
/// <summary>
21+
/// Specifies that this factory method should be used for creator-based deserialization.
22+
/// </summary>
23+
[AttributeUsage(AttributeTargets.Method)]
24+
public class BsonFactoryMethodAttribute : Attribute, IBsonCreatorMapAttribute
25+
{
26+
// private fields
27+
private string[] _argumentNames;
28+
29+
// constructors
30+
/// <summary>
31+
/// Initializes a new instance of the BsonFactoryMethodAttribute class.
32+
/// </summary>
33+
public BsonFactoryMethodAttribute()
34+
{ }
35+
36+
/// <summary>
37+
/// Initializes a new instance of the BsonFactoryMethodAttribute class.
38+
/// </summary>
39+
/// <param name="argumentNames">The names of the members that the creator argument values come from.</param>
40+
public BsonFactoryMethodAttribute(params string[] argumentNames)
41+
{
42+
_argumentNames = argumentNames;
43+
}
44+
45+
// public properties
46+
/// <summary>
47+
/// Gets the names of the members that the creator arguments values come from.
48+
/// </summary>
49+
public string[] ArgumentNames
50+
{
51+
get { return _argumentNames; }
52+
}
53+
54+
// public methods
55+
/// <summary>
56+
/// Applies a modification to the creator map.
57+
/// </summary>
58+
/// <param name="creatorMap">The creator map.</param>
59+
public void Apply(BsonCreatorMap creatorMap)
60+
{
61+
if (_argumentNames != null)
62+
{
63+
creatorMap.SetArguments(_argumentNames);
64+
}
65+
}
66+
}
67+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright 2010-2013 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+
namespace MongoDB.Bson.Serialization
17+
{
18+
/// <summary>
19+
/// Represents an attribute used to modify a creator map.
20+
/// </summary>
21+
public interface IBsonCreatorMapAttribute
22+
{
23+
/// <summary>
24+
/// Applies the attribute to the creator map.
25+
/// </summary>
26+
/// <param name="creatorMap">The creator map.</param>
27+
void Apply(BsonCreatorMap creatorMap);
28+
}
29+
}

0 commit comments

Comments
 (0)