15
15
16
16
using System ;
17
17
using System . Collections . Generic ;
18
- using System . IO ;
19
- using MongoDB . Bson . IO ;
20
18
using MongoDB . Bson . Serialization . Options ;
21
19
22
20
namespace MongoDB . Bson . Serialization . Serializers
@@ -25,7 +23,7 @@ namespace MongoDB.Bson.Serialization.Serializers
25
23
/// Represents a serializer for one-dimensional arrays.
26
24
/// </summary>
27
25
/// <typeparam name="T">The type of the elements.</typeparam>
28
- public class ArraySerializer < T > : BsonBaseSerializer , IBsonArraySerializer
26
+ public class ArraySerializer < T > : EnumerableSerializerBase < T > , IBsonArraySerializer
29
27
{
30
28
// constructors
31
29
/// <summary>
@@ -36,123 +34,56 @@ public ArraySerializer()
36
34
{
37
35
}
38
36
39
- // public methods
37
+ // protected methods
40
38
/// <summary>
41
- /// Deserializes an object from a BsonReader .
39
+ /// Adds the item .
42
40
/// </summary>
43
- /// <param name="bsonReader">The BsonReader.</param>
44
- /// <param name="nominalType">The nominal type of the object.</param>
45
- /// <param name="actualType">The actual type of the object.</param>
46
- /// <param name="options">The serialization options.</param>
47
- /// <returns>An object.</returns>
48
- public override object Deserialize (
49
- BsonReader bsonReader ,
50
- Type nominalType ,
51
- Type actualType ,
52
- IBsonSerializationOptions options )
41
+ /// <param name="instance">The instance.</param>
42
+ /// <param name="item">The item.</param>
43
+ protected override void AddItem ( object instance , T item )
53
44
{
54
- VerifyTypes ( nominalType , actualType , typeof ( T [ ] ) ) ;
55
- var arraySerializationOptions = EnsureSerializationOptions < ArraySerializationOptions > ( options ) ;
56
- var itemSerializationOptions = arraySerializationOptions . ItemSerializationOptions ;
57
-
58
- var bsonType = bsonReader . GetCurrentBsonType ( ) ;
59
- switch ( bsonType )
60
- {
61
- case BsonType . Null :
62
- bsonReader . ReadNull ( ) ;
63
- return null ;
64
- case BsonType . Array :
65
- bsonReader . ReadStartArray ( ) ;
66
- var discriminatorConvention = BsonSerializer . LookupDiscriminatorConvention ( typeof ( T ) ) ;
67
- var list = new List < T > ( ) ;
68
-
69
- var itemNominalType = typeof ( T ) ;
70
- var itemNominalTypeSerializer = BsonSerializer . LookupSerializer ( typeof ( T ) ) ;
71
-
72
- while ( bsonReader . ReadBsonType ( ) != BsonType . EndOfDocument )
73
- {
74
- var elementType = itemNominalType . IsValueType ? itemNominalType : discriminatorConvention . GetActualType ( bsonReader , typeof ( T ) ) ;
75
- var serializer = itemNominalType . IsValueType ? itemNominalTypeSerializer : BsonSerializer . LookupSerializer ( elementType ) ;
76
- var element = ( T ) serializer . Deserialize ( bsonReader , typeof ( T ) , elementType , itemSerializationOptions ) ;
77
- list . Add ( element ) ;
78
- }
79
- bsonReader . ReadEndArray ( ) ;
80
- return list . ToArray ( ) ;
81
- case BsonType . Document :
82
- bsonReader . ReadStartDocument ( ) ;
83
- bsonReader . ReadString ( "_t" ) ; // skip over discriminator
84
- bsonReader . ReadName ( "_v" ) ;
85
- var value = Deserialize ( bsonReader , actualType , actualType , options ) ;
86
- bsonReader . ReadEndDocument ( ) ;
87
- return value ;
88
- default :
89
- var message = string . Format ( "Can't deserialize a {0} from BsonType {1}." , actualType . FullName , bsonType ) ;
90
- throw new FileFormatException ( message ) ;
91
- }
45
+ ( ( List < T > ) instance ) . Add ( item ) ;
92
46
}
93
47
94
48
/// <summary>
95
- /// Gets the serialization info for individual items of an enumerable type .
49
+ /// Creates the instance .
96
50
/// </summary>
97
- /// <returns>The serialization info for the items.</returns>
98
- public BsonSerializationInfo GetItemSerializationInfo ( )
51
+ /// <param name="actualType">The actual type.</param>
52
+ /// <returns>The instance.</returns>
53
+ protected override object CreateInstance ( Type actualType )
99
54
{
100
- string elementName = null ;
101
- var serializer = BsonSerializer . LookupSerializer ( typeof ( T ) ) ;
102
- var nominalType = typeof ( T ) ;
103
- IBsonSerializationOptions serializationOptions = null ;
104
- return new BsonSerializationInfo ( elementName , serializer , nominalType , serializationOptions ) ;
55
+ return new List < T > ( ) ;
105
56
}
106
57
107
58
/// <summary>
108
- /// Serializes an object to a BsonWriter .
59
+ /// Enumerates the items .
109
60
/// </summary>
110
- /// <param name="bsonWriter">The BsonWriter.</param>
111
- /// <param name="nominalType">The nominal type.</param>
112
- /// <param name="value">The object.</param>
113
- /// <param name="options">The serialization options.</param>
114
- public override void Serialize (
115
- BsonWriter bsonWriter ,
116
- Type nominalType ,
117
- object value ,
118
- IBsonSerializationOptions options )
61
+ /// <param name="instance">The instance.</param>
62
+ /// <returns>The items.</returns>
63
+ protected override IEnumerable < T > EnumerateItemsInSerializationOrder ( object instance )
119
64
{
120
- if ( value == null )
121
- {
122
- bsonWriter . WriteNull ( ) ;
123
- }
124
- else
125
- {
126
- var actualType = value . GetType ( ) ;
127
- VerifyTypes ( nominalType , actualType , typeof ( T [ ] ) ) ;
128
-
129
- if ( nominalType == typeof ( object ) )
130
- {
131
- bsonWriter . WriteStartDocument ( ) ;
132
- bsonWriter . WriteString ( "_t" , TypeNameDiscriminator . GetDiscriminator ( actualType ) ) ;
133
- bsonWriter . WriteName ( "_v" ) ;
134
- Serialize ( bsonWriter , actualType , value , options ) ;
135
- bsonWriter . WriteEndDocument ( ) ;
136
- return ;
137
- }
138
-
139
- var array = ( T [ ] ) value ;
140
- var arraySerializationOptions = EnsureSerializationOptions < ArraySerializationOptions > ( options ) ;
141
- var itemSerializationOptions = arraySerializationOptions . ItemSerializationOptions ;
142
-
143
- var itemNominalType = typeof ( T ) ;
144
- var itemNominalTypeSerializer = BsonSerializer . LookupSerializer ( itemNominalType ) ;
145
-
146
- bsonWriter . WriteStartArray ( ) ;
65
+ return ( IEnumerable < T > ) instance ;
66
+ }
147
67
148
- foreach ( var item in array )
149
- {
150
- var itemSerializer = ( itemNominalType . IsValueType || item == null ) ? itemNominalTypeSerializer : BsonSerializer . LookupSerializer ( item . GetType ( ) ) ;
151
- itemSerializer . Serialize ( bsonWriter , itemNominalType , item , itemSerializationOptions ) ;
152
- }
68
+ /// <summary>
69
+ /// Finalizes the result.
70
+ /// </summary>
71
+ /// <param name="instance">The instance.</param>
72
+ /// <param name="actualType">The actual type.</param>
73
+ /// <returns>The result.</returns>
74
+ protected override object FinalizeResult ( object instance , Type actualType )
75
+ {
76
+ return ( ( List < T > ) instance ) . ToArray ( ) ;
77
+ }
153
78
154
- bsonWriter . WriteEndArray ( ) ;
155
- }
79
+ /// <summary>
80
+ /// Verifies the types.
81
+ /// </summary>
82
+ /// <param name="nominalType">Type nominal type.</param>
83
+ /// <param name="actualType">The actual type.</param>
84
+ protected override void VerifyTypes ( Type nominalType , Type actualType )
85
+ {
86
+ VerifyTypes ( nominalType , actualType , typeof ( T [ ] ) ) ;
156
87
}
157
88
}
158
89
}
0 commit comments