1
- using System ;
2
- using System . Collections . Specialized ;
3
- using MongoDB . Bson ;
1
+ using MongoDB . Bson ;
4
2
using MongoDB . Bson . IO ;
5
3
using MongoDB . Bson . Serialization ;
6
4
using MongoDB . Bson . Serialization . Serializers ;
5
+ using System ;
6
+ using System . Collections . Specialized ;
7
7
8
8
namespace Elmah
9
9
{
10
10
public class NameValueCollectionSerializer : BsonBaseSerializer
11
11
{
12
12
private static readonly NameValueCollectionSerializer instance = new NameValueCollectionSerializer ( ) ;
13
+
13
14
public static NameValueCollectionSerializer Instance
14
15
{
15
16
get { return instance ; }
16
17
}
17
18
18
- public override object Deserialize ( BsonReader bsonReader , Type nominalType , Type actualType , IBsonSerializationOptions options )
19
- {
20
- return Deserialize ( bsonReader , nominalType , options ) ;
21
- }
19
+ public override object Deserialize ( BsonReader bsonReader , Type nominalType , Type actualType , IBsonSerializationOptions options )
20
+ {
21
+ return Deserialize ( bsonReader , nominalType , options ) ;
22
+ }
22
23
23
24
public override object Deserialize (
24
25
BsonReader bsonReader ,
25
26
Type nominalType ,
26
27
IBsonSerializationOptions options
27
28
)
28
29
{
30
+ var bsonType = bsonReader . GetCurrentBsonType ( ) ;
31
+ if ( bsonType == BsonType . Null )
32
+ {
33
+ bsonReader . ReadNull ( ) ;
34
+ return null ;
35
+ }
36
+
29
37
var nvc = new NameValueCollection ( ) ;
30
- bsonReader . ReadStartDocument ( ) ;
38
+
39
+ bsonReader . ReadStartArray ( ) ;
31
40
while ( bsonReader . ReadBsonType ( ) != BsonType . EndOfDocument )
32
41
{
33
- var name = bsonReader . ReadName ( ) . Replace ( "__period__" , "." ) ;
34
- var value = bsonReader . ReadString ( ) ;
35
- nvc . Add ( name , value ) ;
42
+ bsonReader . ReadStartArray ( ) ;
43
+ var key = ( string ) StringSerializer . Instance . Deserialize ( bsonReader , typeof ( string ) , options ) ;
44
+ var val = ( string ) StringSerializer . Instance . Deserialize ( bsonReader , typeof ( string ) , options ) ;
45
+ bsonReader . ReadEndArray ( ) ;
46
+ nvc . Add ( key , val ) ;
36
47
}
37
- bsonReader . ReadEndDocument ( ) ;
48
+ bsonReader . ReadEndArray ( ) ;
49
+
38
50
return nvc ;
39
51
}
40
52
@@ -45,21 +57,26 @@ public override void Serialize(
45
57
IBsonSerializationOptions options
46
58
)
47
59
{
60
+ if ( value == null )
61
+ {
62
+ bsonWriter . WriteNull ( ) ;
63
+ return ;
64
+ }
65
+
48
66
var nvc = ( NameValueCollection ) value ;
49
- bsonWriter . WriteStartDocument ( ) ;
50
- if ( nvc != null && nvc . Count > 0 )
67
+
68
+ bsonWriter . WriteStartArray ( ) ;
69
+ foreach ( var key in nvc . AllKeys )
51
70
{
52
- foreach ( var key in nvc . AllKeys )
71
+ foreach ( var val in nvc . GetValues ( key ) )
53
72
{
54
- if ( string . IsNullOrEmpty ( key ) )
55
- {
56
- continue ;
57
- }
58
-
59
- bsonWriter . WriteString ( key . Replace ( "." , "__period__" ) , nvc [ key ] ) ;
73
+ bsonWriter . WriteStartArray ( ) ;
74
+ StringSerializer . Instance . Serialize ( bsonWriter , typeof ( string ) , key , options ) ;
75
+ StringSerializer . Instance . Serialize ( bsonWriter , typeof ( string ) , val , options ) ;
76
+ bsonWriter . WriteEndArray ( ) ;
60
77
}
61
78
}
62
- bsonWriter . WriteEndDocument ( ) ;
79
+ bsonWriter . WriteEndArray ( ) ;
63
80
}
64
81
}
65
82
}
0 commit comments