@@ -41,7 +41,6 @@ static BsonDefaultSerializationProvider()
41
41
{
42
42
__serializers = new Dictionary < Type , IBsonSerializer >
43
43
{
44
- { typeof ( ArrayList ) , EnumerableSerializer . Instance } ,
45
44
{ typeof ( BitArray ) , BitArraySerializer . Instance } ,
46
45
{ typeof ( Bitmap ) , BitmapSerializer . Instance } ,
47
46
{ typeof ( Boolean ) , BooleanSerializer . Instance } ,
@@ -76,26 +75,18 @@ static BsonDefaultSerializationProvider()
76
75
{ typeof ( Double ) , DoubleSerializer . Instance } ,
77
76
{ typeof ( System . Drawing . Size ) , DrawingSizeSerializer . Instance } ,
78
77
{ typeof ( Guid ) , GuidSerializer . Instance } ,
79
- { typeof ( Hashtable ) , DictionarySerializer . Instance } ,
80
78
{ typeof ( IBsonSerializable ) , BsonIBsonSerializableSerializer . Instance } ,
81
- { typeof ( ICollection ) , EnumerableSerializer . Instance } ,
82
- { typeof ( IDictionary ) , DictionarySerializer . Instance } ,
83
- { typeof ( IEnumerable ) , EnumerableSerializer . Instance } ,
84
- { typeof ( IList ) , EnumerableSerializer . Instance } ,
85
79
{ typeof ( Image ) , ImageSerializer . Instance } ,
86
80
{ typeof ( Int16 ) , Int16Serializer . Instance } ,
87
81
{ typeof ( Int32 ) , Int32Serializer . Instance } ,
88
82
{ typeof ( Int64 ) , Int64Serializer . Instance } ,
89
83
{ typeof ( IPAddress ) , IPAddressSerializer . Instance } ,
90
84
{ typeof ( IPEndPoint ) , IPEndPointSerializer . Instance } ,
91
- { typeof ( ListDictionary ) , DictionarySerializer . Instance } ,
92
85
{ typeof ( Object ) , ObjectSerializer . Instance } ,
93
86
{ typeof ( ObjectId ) , ObjectIdSerializer . Instance } ,
94
- { typeof ( OrderedDictionary ) , DictionarySerializer . Instance } ,
95
87
{ typeof ( Queue ) , QueueSerializer . Instance } ,
96
88
{ typeof ( SByte ) , SByteSerializer . Instance } ,
97
89
{ typeof ( Single ) , SingleSerializer . Instance } ,
98
- { typeof ( SortedList ) , DictionarySerializer . Instance } ,
99
90
{ typeof ( Stack ) , StackSerializer . Instance } ,
100
91
{ typeof ( String ) , StringSerializer . Instance } ,
101
92
{ typeof ( TimeSpan ) , TimeSpanSerializer . Instance } ,
@@ -108,20 +99,9 @@ static BsonDefaultSerializationProvider()
108
99
109
100
__genericSerializerDefinitions = new Dictionary < Type , Type >
110
101
{
111
- { typeof ( Collection < > ) , typeof ( EnumerableSerializer < > ) } ,
112
- { typeof ( Dictionary < , > ) , typeof ( DictionarySerializer < , > ) } ,
113
- { typeof ( HashSet < > ) , typeof ( EnumerableSerializer < > ) } ,
114
- { typeof ( ICollection < > ) , typeof ( EnumerableSerializer < > ) } ,
115
- { typeof ( IDictionary < , > ) , typeof ( DictionarySerializer < , > ) } ,
116
- { typeof ( IEnumerable < > ) , typeof ( EnumerableSerializer < > ) } ,
117
- { typeof ( IList < > ) , typeof ( EnumerableSerializer < > ) } ,
118
- { typeof ( LinkedList < > ) , typeof ( EnumerableSerializer < > ) } ,
119
- { typeof ( List < > ) , typeof ( EnumerableSerializer < > ) } ,
102
+ { typeof ( KeyValuePair < , > ) , typeof ( KeyValuePairSerializer < , > ) } ,
120
103
{ typeof ( Nullable < > ) , typeof ( NullableSerializer < > ) } ,
121
- { typeof ( ObservableCollection < > ) , typeof ( EnumerableSerializer < > ) } ,
122
104
{ typeof ( Queue < > ) , typeof ( QueueSerializer < > ) } ,
123
- { typeof ( SortedDictionary < , > ) , typeof ( DictionarySerializer < , > ) } ,
124
- { typeof ( SortedList < , > ) , typeof ( DictionarySerializer < , > ) } ,
125
105
{ typeof ( Stack < > ) , typeof ( StackSerializer < > ) }
126
106
} ;
127
107
}
@@ -187,6 +167,82 @@ public IBsonSerializer GetSerializer(Type type)
187
167
return EnumSerializer . Instance ;
188
168
}
189
169
170
+ // classes that implement IDictionary or IEnumerable are serialized using either DictionarySerializer or EnumerableSerializer
171
+ // this does mean that any additional public properties the class might have won't be serialized (just like the XmlSerializer)
172
+ var collectionSerializer = GetCollectionSerializer ( type ) ;
173
+ if ( collectionSerializer != null )
174
+ {
175
+ return collectionSerializer ;
176
+ }
177
+
178
+ return null ;
179
+ }
180
+
181
+ // private methods
182
+ private IBsonSerializer GetCollectionSerializer ( Type type )
183
+ {
184
+ Type implementedGenericDictionaryInterface = null ;
185
+ Type implementedGenericEnumerableInterface = null ;
186
+ Type implementedDictionaryInterface = null ;
187
+ Type implementedEnumerableInterface = null ;
188
+
189
+ var implementedInterfaces = new List < Type > ( type . GetInterfaces ( ) ) ;
190
+ if ( type . IsInterface )
191
+ {
192
+ implementedInterfaces . Add ( type ) ;
193
+ }
194
+ foreach ( var implementedInterface in implementedInterfaces )
195
+ {
196
+ if ( implementedInterface . IsGenericType )
197
+ {
198
+ var genericInterfaceDefinition = implementedInterface . GetGenericTypeDefinition ( ) ;
199
+ if ( genericInterfaceDefinition == typeof ( IDictionary < , > ) )
200
+ {
201
+ implementedGenericDictionaryInterface = implementedInterface ;
202
+ }
203
+ if ( genericInterfaceDefinition == typeof ( IEnumerable < > ) )
204
+ {
205
+ implementedGenericEnumerableInterface = implementedInterface ;
206
+ }
207
+ }
208
+ else
209
+ {
210
+ if ( implementedInterface == typeof ( IDictionary ) )
211
+ {
212
+ implementedDictionaryInterface = implementedInterface ;
213
+ }
214
+ if ( implementedInterface == typeof ( IEnumerable ) )
215
+ {
216
+ implementedEnumerableInterface = implementedInterface ;
217
+ }
218
+ }
219
+ }
220
+
221
+ // the order of the tests is important
222
+ if ( implementedGenericDictionaryInterface != null )
223
+ {
224
+ var keyType = implementedGenericDictionaryInterface . GetGenericArguments ( ) [ 0 ] ;
225
+ var valueType = implementedGenericDictionaryInterface . GetGenericArguments ( ) [ 1 ] ;
226
+ var genericSerializerDefinition = typeof ( DictionarySerializer < , > ) ;
227
+ var genericSerializerType = genericSerializerDefinition . MakeGenericType ( keyType , valueType ) ;
228
+ return ( IBsonSerializer ) Activator . CreateInstance ( genericSerializerType ) ;
229
+ }
230
+ else if ( implementedDictionaryInterface != null )
231
+ {
232
+ return DictionarySerializer . Instance ;
233
+ }
234
+ else if ( implementedGenericEnumerableInterface != null )
235
+ {
236
+ var valueType = implementedGenericEnumerableInterface . GetGenericArguments ( ) [ 0 ] ;
237
+ var genericSerializerDefinition = typeof ( EnumerableSerializer < > ) ;
238
+ var genericSerializerType = genericSerializerDefinition . MakeGenericType ( valueType ) ;
239
+ return ( IBsonSerializer ) Activator . CreateInstance ( genericSerializerType ) ;
240
+ }
241
+ else if ( implementedEnumerableInterface != null )
242
+ {
243
+ return EnumerableSerializer . Instance ;
244
+ }
245
+
190
246
return null ;
191
247
}
192
248
}
0 commit comments