Skip to content

Commit afb637a

Browse files
Serialize NameValueCollection as ArrayOfArrays to handle nulls, non-valid BSON names etc...
1 parent 5f51350 commit afb637a

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed
Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1-
using System;
2-
using System.Collections.Specialized;
3-
using MongoDB.Bson;
1+
using MongoDB.Bson;
42
using MongoDB.Bson.IO;
53
using MongoDB.Bson.Serialization;
64
using MongoDB.Bson.Serialization.Serializers;
5+
using System;
6+
using System.Collections.Specialized;
77

88
namespace Elmah
99
{
1010
public class NameValueCollectionSerializer : BsonBaseSerializer
1111
{
1212
private static readonly NameValueCollectionSerializer instance = new NameValueCollectionSerializer();
13+
1314
public static NameValueCollectionSerializer Instance
1415
{
1516
get { return instance; }
1617
}
1718

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+
}
2223

2324
public override object Deserialize(
2425
BsonReader bsonReader,
2526
Type nominalType,
2627
IBsonSerializationOptions options
2728
)
2829
{
30+
var bsonType = bsonReader.GetCurrentBsonType();
31+
if (bsonType == BsonType.Null)
32+
{
33+
bsonReader.ReadNull();
34+
return null;
35+
}
36+
2937
var nvc = new NameValueCollection();
30-
bsonReader.ReadStartDocument();
38+
39+
bsonReader.ReadStartArray();
3140
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
3241
{
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);
3647
}
37-
bsonReader.ReadEndDocument();
48+
bsonReader.ReadEndArray();
49+
3850
return nvc;
3951
}
4052

@@ -45,21 +57,26 @@ public override void Serialize(
4557
IBsonSerializationOptions options
4658
)
4759
{
60+
if (value == null)
61+
{
62+
bsonWriter.WriteNull();
63+
return;
64+
}
65+
4866
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)
5170
{
52-
foreach (var key in nvc.AllKeys)
71+
foreach (var val in nvc.GetValues(key))
5372
{
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();
6077
}
6178
}
62-
bsonWriter.WriteEndDocument();
79+
bsonWriter.WriteEndArray();
6380
}
6481
}
6582
}

0 commit comments

Comments
 (0)