@@ -58,14 +58,15 @@ public class DictionarySerializationOptions : BsonBaseSerializationOptions
58
58
59
59
// private fields
60
60
private DictionaryRepresentation _representation = DictionaryRepresentation . Dynamic ;
61
- private IBsonSerializationOptions _valueSerializationOptions ;
61
+ private KeyValuePairSerializationOptions _keyValuePairSerializationOptions ;
62
62
63
63
// constructors
64
64
/// <summary>
65
65
/// Initializes a new instance of the DictionarySerializationOptions class.
66
66
/// </summary>
67
67
public DictionarySerializationOptions ( )
68
68
{
69
+ _keyValuePairSerializationOptions = ( KeyValuePairSerializationOptions ) KeyValuePairSerializationOptions . Defaults . Clone ( ) ;
69
70
}
70
71
71
72
/// <summary>
@@ -75,17 +76,22 @@ public DictionarySerializationOptions()
75
76
public DictionarySerializationOptions ( DictionaryRepresentation representation )
76
77
{
77
78
_representation = representation ;
79
+ _keyValuePairSerializationOptions = ( KeyValuePairSerializationOptions ) KeyValuePairSerializationOptions . Defaults . Clone ( ) ;
78
80
}
79
81
80
82
/// <summary>
81
83
/// Initializes a new instance of the DictionarySerializationOptions class.
82
84
/// </summary>
83
85
/// <param name="representation">The representation to use for a Dictionary.</param>
84
- /// <param name="valueSerializationOptions ">The serialization options for the values in the dictionary.</param>
85
- public DictionarySerializationOptions ( DictionaryRepresentation representation , IBsonSerializationOptions valueSerializationOptions )
86
+ /// <param name="keyValuePairSerializationOptions ">The serialization options for the key/value pairs in the dictionary.</param>
87
+ public DictionarySerializationOptions ( DictionaryRepresentation representation , KeyValuePairSerializationOptions keyValuePairSerializationOptions )
86
88
{
89
+ if ( keyValuePairSerializationOptions == null )
90
+ {
91
+ throw new ArgumentNullException ( "keyValuePairSerializationOptions" ) ;
92
+ }
87
93
_representation = representation ;
88
- _valueSerializationOptions = valueSerializationOptions ;
94
+ _keyValuePairSerializationOptions = keyValuePairSerializationOptions ;
89
95
}
90
96
91
97
// public static properties
@@ -111,7 +117,13 @@ public static DictionarySerializationOptions ArrayOfDocuments
111
117
public static DictionarySerializationOptions Defaults
112
118
{
113
119
get { return __defaults ; }
114
- set { __defaults = value ; }
120
+ set {
121
+ if ( value == null )
122
+ {
123
+ throw new ArgumentNullException ( "value" ) ;
124
+ }
125
+ __defaults = value ;
126
+ }
115
127
}
116
128
117
129
/// <summary>
@@ -134,11 +146,20 @@ public static DictionarySerializationOptions Dynamic
134
146
/// <summary>
135
147
/// Gets or sets the serialization options for the values in the dictionary.
136
148
/// </summary>
137
- [ Obsolete ( "Use ValueSerializationOptions instead." ) ]
149
+ [ Obsolete ( "Use KeyValuePairSerializationOptions instead." ) ]
138
150
public IBsonSerializationOptions ItemSerializationOptions
139
151
{
140
- get { return ValueSerializationOptions ; }
141
- set { ValueSerializationOptions = value ; }
152
+ get { return _keyValuePairSerializationOptions . ValueSerializationOptions ; }
153
+ set {
154
+ if ( value == null )
155
+ {
156
+ throw new ArgumentNullException ( "value" ) ;
157
+ }
158
+ _keyValuePairSerializationOptions = new KeyValuePairSerializationOptions (
159
+ _keyValuePairSerializationOptions . Representation ,
160
+ _keyValuePairSerializationOptions . KeySerializationOptions ,
161
+ value ) ;
162
+ }
142
163
}
143
164
144
165
/// <summary>
@@ -157,13 +178,17 @@ public DictionaryRepresentation Representation
157
178
/// <summary>
158
179
/// Gets or sets the serialization options for the values in the dictionary.
159
180
/// </summary>
160
- public IBsonSerializationOptions ValueSerializationOptions
181
+ public KeyValuePairSerializationOptions KeyValuePairSerializationOptions
161
182
{
162
- get { return _valueSerializationOptions ; }
183
+ get { return _keyValuePairSerializationOptions ; }
163
184
set
164
185
{
186
+ if ( value == null )
187
+ {
188
+ throw new ArgumentNullException ( "value" ) ;
189
+ }
165
190
EnsureNotFrozen ( ) ;
166
- _valueSerializationOptions = value ;
191
+ _keyValuePairSerializationOptions = value ;
167
192
}
168
193
}
169
194
@@ -176,6 +201,7 @@ public IBsonSerializationOptions ValueSerializationOptions
176
201
public override void ApplyAttribute ( IBsonSerializer serializer , Attribute attribute )
177
202
{
178
203
EnsureNotFrozen ( ) ;
204
+
179
205
var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute ;
180
206
if ( dictionaryOptionsAttribute != null )
181
207
{
@@ -198,14 +224,16 @@ public override void ApplyAttribute(IBsonSerializer serializer, Attribute attrib
198
224
}
199
225
}
200
226
227
+ // any other attributes are applied to the values
201
228
var valueType = typeof ( object ) ;
202
229
if ( serializer . GetType ( ) . IsGenericType )
203
230
{
204
- valueType = serializer . GetType ( ) . GetGenericArguments ( ) [ 1 ] ;
231
+ valueType = serializer . GetType ( ) . GetGenericArguments ( ) [ 1 ] ; // TValue
205
232
}
206
233
var valueSerializer = BsonSerializer . LookupSerializer ( valueType ) ;
207
234
208
- if ( _valueSerializationOptions == null )
235
+ var valueSerializationOptions = _keyValuePairSerializationOptions . ValueSerializationOptions ;
236
+ if ( valueSerializationOptions == null )
209
237
{
210
238
var valueDefaultSerializationOptions = valueSerializer . GetDefaultSerializationOptions ( ) ;
211
239
@@ -227,10 +255,14 @@ public override void ApplyAttribute(IBsonSerializer serializer, Attribute attrib
227
255
throw new NotSupportedException ( message ) ;
228
256
}
229
257
230
- _valueSerializationOptions = valueDefaultSerializationOptions . Clone ( ) ;
258
+ valueSerializationOptions = valueDefaultSerializationOptions . Clone ( ) ;
231
259
}
232
260
233
- _valueSerializationOptions . ApplyAttribute ( valueSerializer , attribute ) ;
261
+ valueSerializationOptions . ApplyAttribute ( valueSerializer , attribute ) ;
262
+ _keyValuePairSerializationOptions = new KeyValuePairSerializationOptions (
263
+ _keyValuePairSerializationOptions . Representation ,
264
+ _keyValuePairSerializationOptions . KeySerializationOptions ,
265
+ valueSerializationOptions ) ;
234
266
}
235
267
236
268
/// <summary>
@@ -239,7 +271,7 @@ public override void ApplyAttribute(IBsonSerializer serializer, Attribute attrib
239
271
/// <returns>A cloned copy of the serialization options.</returns>
240
272
public override IBsonSerializationOptions Clone ( )
241
273
{
242
- return new DictionarySerializationOptions ( _representation , _valueSerializationOptions ) ;
274
+ return new DictionarySerializationOptions ( _representation , _keyValuePairSerializationOptions ) ;
243
275
}
244
276
245
277
/// <summary>
@@ -250,9 +282,9 @@ public override IBsonSerializationOptions Freeze()
250
282
{
251
283
if ( ! IsFrozen )
252
284
{
253
- if ( _valueSerializationOptions != null )
285
+ if ( _keyValuePairSerializationOptions != null )
254
286
{
255
- _valueSerializationOptions . Freeze ( ) ;
287
+ _keyValuePairSerializationOptions . Freeze ( ) ;
256
288
}
257
289
}
258
290
return base . Freeze ( ) ;
0 commit comments