From 4ffd957cf4d23dcd3fb5296e37d39fd9eb5b44fd Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Thu, 15 Nov 2012 10:26:21 -0600 Subject: [PATCH 1/7] Maintain a null key by doing a substitution Maintain a null key by doing a substitution before it's serialized for MongoDB. Also, prevent issues with the illegal starting character: "$". --- .../NameValueCollectionSerializer.cs | 131 +++++++++--------- 1 file changed, 67 insertions(+), 64 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index 8dba482..04f45b6 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -1,65 +1,68 @@ -using System; -using System.Collections.Specialized; -using MongoDB.Bson; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.Serializers; - -namespace Elmah -{ - public class NameValueCollectionSerializer : BsonBaseSerializer - { - private static readonly NameValueCollectionSerializer instance = new NameValueCollectionSerializer(); - public static NameValueCollectionSerializer Instance - { - get { return instance; } - } - - public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) - { - return Deserialize(bsonReader, nominalType, options); - } - - public override object Deserialize( - BsonReader bsonReader, - Type nominalType, - IBsonSerializationOptions options - ) - { - var nvc = new NameValueCollection(); - bsonReader.ReadStartDocument(); - while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) - { - var name = bsonReader.ReadName().Replace("__period__", "."); - var value = bsonReader.ReadString(); - nvc.Add(name, value); - } - bsonReader.ReadEndDocument(); - return nvc; - } - - public override void Serialize( - BsonWriter bsonWriter, - Type nominalType, - object value, - IBsonSerializationOptions options - ) - { - var nvc = (NameValueCollection)value; - bsonWriter.WriteStartDocument(); - if (nvc != null && nvc.Count > 0) - { - foreach (var key in nvc.AllKeys) - { - if (string.IsNullOrWhiteSpace(key)) - { - continue; - } - - bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]); - } - } - bsonWriter.WriteEndDocument(); - } - } +using System; +using System.Collections.Specialized; +using MongoDB.Bson; +using MongoDB.Bson.IO; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; + +namespace Elmah +{ + public class NameValueCollectionSerializer : BsonBaseSerializer + { + private static readonly NameValueCollectionSerializer instance = new NameValueCollectionSerializer(); + public static NameValueCollectionSerializer Instance + { + get { return instance; } + } + + public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) + { + return Deserialize(bsonReader, nominalType, options); + } + + public override object Deserialize( + BsonReader bsonReader, + Type nominalType, + IBsonSerializationOptions options + ) + { + var nvc = new NameValueCollection(); + bsonReader.ReadStartDocument(); + while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) + { + var name = bsonReader.ReadName(); + var originalName = (name == "__null__") ? null : name.Replace("__period__", ".").Replace("__dollarsign__", "$"); + var value = bsonReader.ReadString(); + nvc.Add(originalName, value); + } + bsonReader.ReadEndDocument(); + return nvc; + } + + public override void Serialize( + BsonWriter bsonWriter, + Type nominalType, + object value, + IBsonSerializationOptions options + ) + { + var nvc = (NameValueCollection)value; + bsonWriter.WriteStartDocument(); + if (nvc != null && nvc.Count > 0) + { + foreach (var key in nvc.AllKeys) + { + var legalKey = key ?? "__null__"; + legalKey = legalKey.Replace(".", "__period__"); + if (legalKey.StartsWith("$")) + { + legalKey = "__dollarsign__" + legalKey.Substring(1); + } + + bsonWriter.WriteString(legalKey, nvc[key]); + } + } + bsonWriter.WriteEndDocument(); + } + } } \ No newline at end of file From 561fb43a4e0c6c710c152f7b57b1fa4bd0726592 Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Thu, 15 Nov 2012 11:10:31 -0600 Subject: [PATCH 2/7] Revert "Maintain a null key by doing a substitution" This reverts commit 4ffd957cf4d23dcd3fb5296e37d39fd9eb5b44fd. --- .../NameValueCollectionSerializer.cs | 131 +++++++++--------- 1 file changed, 64 insertions(+), 67 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index 04f45b6..8dba482 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -1,68 +1,65 @@ -using System; -using System.Collections.Specialized; -using MongoDB.Bson; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.Serializers; - -namespace Elmah -{ - public class NameValueCollectionSerializer : BsonBaseSerializer - { - private static readonly NameValueCollectionSerializer instance = new NameValueCollectionSerializer(); - public static NameValueCollectionSerializer Instance - { - get { return instance; } - } - - public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) - { - return Deserialize(bsonReader, nominalType, options); - } - - public override object Deserialize( - BsonReader bsonReader, - Type nominalType, - IBsonSerializationOptions options - ) - { - var nvc = new NameValueCollection(); - bsonReader.ReadStartDocument(); - while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) - { - var name = bsonReader.ReadName(); - var originalName = (name == "__null__") ? null : name.Replace("__period__", ".").Replace("__dollarsign__", "$"); - var value = bsonReader.ReadString(); - nvc.Add(originalName, value); - } - bsonReader.ReadEndDocument(); - return nvc; - } - - public override void Serialize( - BsonWriter bsonWriter, - Type nominalType, - object value, - IBsonSerializationOptions options - ) - { - var nvc = (NameValueCollection)value; - bsonWriter.WriteStartDocument(); - if (nvc != null && nvc.Count > 0) - { - foreach (var key in nvc.AllKeys) - { - var legalKey = key ?? "__null__"; - legalKey = legalKey.Replace(".", "__period__"); - if (legalKey.StartsWith("$")) - { - legalKey = "__dollarsign__" + legalKey.Substring(1); - } - - bsonWriter.WriteString(legalKey, nvc[key]); - } - } - bsonWriter.WriteEndDocument(); - } - } +using System; +using System.Collections.Specialized; +using MongoDB.Bson; +using MongoDB.Bson.IO; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; + +namespace Elmah +{ + public class NameValueCollectionSerializer : BsonBaseSerializer + { + private static readonly NameValueCollectionSerializer instance = new NameValueCollectionSerializer(); + public static NameValueCollectionSerializer Instance + { + get { return instance; } + } + + public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) + { + return Deserialize(bsonReader, nominalType, options); + } + + public override object Deserialize( + BsonReader bsonReader, + Type nominalType, + IBsonSerializationOptions options + ) + { + var nvc = new NameValueCollection(); + bsonReader.ReadStartDocument(); + while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) + { + var name = bsonReader.ReadName().Replace("__period__", "."); + var value = bsonReader.ReadString(); + nvc.Add(name, value); + } + bsonReader.ReadEndDocument(); + return nvc; + } + + public override void Serialize( + BsonWriter bsonWriter, + Type nominalType, + object value, + IBsonSerializationOptions options + ) + { + var nvc = (NameValueCollection)value; + bsonWriter.WriteStartDocument(); + if (nvc != null && nvc.Count > 0) + { + foreach (var key in nvc.AllKeys) + { + if (string.IsNullOrWhiteSpace(key)) + { + continue; + } + + bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]); + } + } + bsonWriter.WriteEndDocument(); + } + } } \ No newline at end of file From 2b08343f0b750995d2889c6a3a13801b23c34c6a Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Thu, 15 Nov 2012 11:11:59 -0600 Subject: [PATCH 3/7] Maintain a null key by doing a substitution Maintain a null key by doing a substitution before it's serialized for MongoDB. Also, prevent issues with the illegal starting character: "$". --- .../NameValueCollectionSerializer.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index 8dba482..7ee7970 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -30,9 +30,10 @@ IBsonSerializationOptions options bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { - var name = bsonReader.ReadName().Replace("__period__", "."); + var name = bsonReader.ReadName(); + var originalName = (name == "__null__") ? null : name.Replace("__period__", ".").Replace("__dollarsign__", "$"); var value = bsonReader.ReadString(); - nvc.Add(name, value); + nvc.Add(originalName, value); } bsonReader.ReadEndDocument(); return nvc; @@ -51,12 +52,14 @@ IBsonSerializationOptions options { foreach (var key in nvc.AllKeys) { - if (string.IsNullOrWhiteSpace(key)) - { - continue; - } + var legalKey = key ?? "__null__"; + legalKey = legalKey.Replace(".", "__period__"); + if (legalKey.StartsWith("$")) + { + legalKey = "__dollarsign__" + legalKey.Substring(1); + } - bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]); + bsonWriter.WriteString(legalKey, nvc[key]); } } bsonWriter.WriteEndDocument(); From 37b9df647b94c01d36c7452f44a63dcceb698449 Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Tue, 5 Feb 2013 15:33:51 -0600 Subject: [PATCH 4/7] Revert "Maintain a null key by doing a substitution" This reverts commit 2b08343f0b750995d2889c6a3a13801b23c34c6a. --- .../NameValueCollectionSerializer.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index 7ee7970..8dba482 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -30,10 +30,9 @@ IBsonSerializationOptions options bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { - var name = bsonReader.ReadName(); - var originalName = (name == "__null__") ? null : name.Replace("__period__", ".").Replace("__dollarsign__", "$"); + var name = bsonReader.ReadName().Replace("__period__", "."); var value = bsonReader.ReadString(); - nvc.Add(originalName, value); + nvc.Add(name, value); } bsonReader.ReadEndDocument(); return nvc; @@ -52,14 +51,12 @@ IBsonSerializationOptions options { foreach (var key in nvc.AllKeys) { - var legalKey = key ?? "__null__"; - legalKey = legalKey.Replace(".", "__period__"); - if (legalKey.StartsWith("$")) - { - legalKey = "__dollarsign__" + legalKey.Substring(1); - } + if (string.IsNullOrWhiteSpace(key)) + { + continue; + } - bsonWriter.WriteString(legalKey, nvc[key]); + bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]); } } bsonWriter.WriteEndDocument(); From fa381dc7c9f2e894adbeb89df9f089b08d3060d6 Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Tue, 5 Feb 2013 15:35:14 -0600 Subject: [PATCH 5/7] Revert "Revert "Maintain a null key by doing a substitution"" This reverts commit 37b9df647b94c01d36c7452f44a63dcceb698449. --- .../NameValueCollectionSerializer.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index 8dba482..7ee7970 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -30,9 +30,10 @@ IBsonSerializationOptions options bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { - var name = bsonReader.ReadName().Replace("__period__", "."); + var name = bsonReader.ReadName(); + var originalName = (name == "__null__") ? null : name.Replace("__period__", ".").Replace("__dollarsign__", "$"); var value = bsonReader.ReadString(); - nvc.Add(name, value); + nvc.Add(originalName, value); } bsonReader.ReadEndDocument(); return nvc; @@ -51,12 +52,14 @@ IBsonSerializationOptions options { foreach (var key in nvc.AllKeys) { - if (string.IsNullOrWhiteSpace(key)) - { - continue; - } + var legalKey = key ?? "__null__"; + legalKey = legalKey.Replace(".", "__period__"); + if (legalKey.StartsWith("$")) + { + legalKey = "__dollarsign__" + legalKey.Substring(1); + } - bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]); + bsonWriter.WriteString(legalKey, nvc[key]); } } bsonWriter.WriteEndDocument(); From c499987e9caf06558bdbef3cd2c8ce341f932022 Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Tue, 5 Feb 2013 18:23:31 -0600 Subject: [PATCH 6/7] Revert "Revert "Revert "Maintain a null key by doing a substitution""" This reverts commit fa381dc7c9f2e894adbeb89df9f089b08d3060d6. --- .../NameValueCollectionSerializer.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index 7ee7970..8dba482 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -30,10 +30,9 @@ IBsonSerializationOptions options bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { - var name = bsonReader.ReadName(); - var originalName = (name == "__null__") ? null : name.Replace("__period__", ".").Replace("__dollarsign__", "$"); + var name = bsonReader.ReadName().Replace("__period__", "."); var value = bsonReader.ReadString(); - nvc.Add(originalName, value); + nvc.Add(name, value); } bsonReader.ReadEndDocument(); return nvc; @@ -52,14 +51,12 @@ IBsonSerializationOptions options { foreach (var key in nvc.AllKeys) { - var legalKey = key ?? "__null__"; - legalKey = legalKey.Replace(".", "__period__"); - if (legalKey.StartsWith("$")) - { - legalKey = "__dollarsign__" + legalKey.Substring(1); - } + if (string.IsNullOrWhiteSpace(key)) + { + continue; + } - bsonWriter.WriteString(legalKey, nvc[key]); + bsonWriter.WriteString(key.Replace(".", "__period__"), nvc[key]); } } bsonWriter.WriteEndDocument(); From 3e5ddcc1d946c8f914eed25c7c9301fbd80bbae8 Mon Sep 17 00:00:00 2001 From: Brian Beard Date: Tue, 5 Feb 2013 18:56:42 -0600 Subject: [PATCH 7/7] Fix null reference bug in serializer --- src/Elmah.MongoDB/NameValueCollectionSerializer.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs index b19b31b..1056295 100644 --- a/src/Elmah.MongoDB/NameValueCollectionSerializer.cs +++ b/src/Elmah.MongoDB/NameValueCollectionSerializer.cs @@ -57,18 +57,17 @@ public override void Serialize( IBsonSerializationOptions options ) { - if (value == null) + var nvc = value as NameValueCollection; + if (nvc == null) { bsonWriter.WriteNull(); return; } - var nvc = (NameValueCollection)value; - bsonWriter.WriteStartArray(); foreach (var key in nvc.AllKeys) { - foreach (var val in nvc.GetValues(key)) + foreach (var val in nvc.GetValues(key) ?? new string[0]) { bsonWriter.WriteStartArray(); StringSerializer.Instance.Serialize(bsonWriter, typeof(string), key, options);