Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'csharp562v18'

  • Loading branch information...
commit 5825955911d86b2adfd27a60099a7a6915395b60 2 parents e13c7ba + cf64c55
rstam authored

Showing 54 changed files with 703 additions and 978 deletions. Show diff stats Hide diff stats

  1. +5 15 MongoDB.Bson/ObjectModel/BsonArray.cs
  2. +30 144 MongoDB.Bson/ObjectModel/BsonDocument.cs
  3. +27 20 MongoDB.Bson/ObjectModel/BsonDocumentWrapper.cs
  4. +0 44 MongoDB.Bson/ObjectModel/BsonElement.cs
  5. +4 153 MongoDB.Bson/ObjectModel/BsonValue.cs
  6. +5 0 MongoDB.Bson/Serialization/BsonDefaultSerializationProvider.cs
  7. +10 0 MongoDB.Bson/Serialization/BsonMemberMap.cs
  8. +22 2 MongoDB.Bson/Serialization/BsonSerializer.cs
  9. +2 1  MongoDB.Bson/Serialization/Conventions/DiscriminatorConventions.cs
  10. +22 13 MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs
  11. +26 32 MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs
  12. +10 15 MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs
  13. +67 18 MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs
  14. +10 175 MongoDB.Bson/Serialization/Serializers/BsonDateTimeSerializer.cs
  15. +118 7 MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs
  16. +20 3 MongoDB.Bson/Serialization/Serializers/BsonDocumentWrapperSerializer.cs
  17. +11 15 MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs
  18. +11 15 MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs
  19. +11 15 MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs
  20. +12 14 MongoDB.Bson/Serialization/Serializers/BsonJavaScriptSerializer.cs
  21. +14 16 MongoDB.Bson/Serialization/Serializers/BsonJavaScriptWithScopeSerializer.cs
  22. +12 13 MongoDB.Bson/Serialization/Serializers/BsonMaxKeySerializer.cs
  23. +12 13 MongoDB.Bson/Serialization/Serializers/BsonMinKeySerializer.cs
  24. +5 23 MongoDB.Bson/Serialization/Serializers/BsonNullSerializer.cs
  25. +11 15 MongoDB.Bson/Serialization/Serializers/BsonObjectIdSerializer.cs
  26. +11 15 MongoDB.Bson/Serialization/Serializers/BsonRegularExpressionSerializer.cs
  27. +11 15 MongoDB.Bson/Serialization/Serializers/BsonStringSerializer.cs
  28. +3 25 MongoDB.Bson/Serialization/Serializers/BsonSymbolSerializer.cs
  29. +11 15 MongoDB.Bson/Serialization/Serializers/BsonTimestampSerializer.cs
  30. +11 13 MongoDB.Bson/Serialization/Serializers/BsonUndefinedSerializer.cs
  31. +47 11 MongoDB.Bson/Serialization/Serializers/BsonValueSerializer.cs
  32. +1 1  MongoDB.Bson/Serialization/Serializers/DecimalSerializer.cs
  33. +15 14 MongoDB.BsonUnitTests/IO/BsonDocumentReaderTests.cs
  34. +4 3 MongoDB.BsonUnitTests/IO/BsonRoundTripTests.cs
  35. +0 4 MongoDB.BsonUnitTests/IO/JsonWriterTests.cs
  36. +6 11 MongoDB.BsonUnitTests/Jira/CSharp446Tests.cs
  37. +12 11 MongoDB.BsonUnitTests/ObjectModel/BsonDocumentTests.cs
  38. +62 39 MongoDB.BsonUnitTests/Serialization/Serializers/BsonValueSerializerTests.cs
  39. +1 1  MongoDB.BsonUnitTests/Serialization/Serializers/CircularReferencesTests.cs
  40. +2 1  MongoDB.Driver/Builders/CollectionOptionsBuilder.cs
  41. +2 1  MongoDB.Driver/Builders/FieldsBuilder.cs
  42. +2 1  MongoDB.Driver/Builders/GeoHaystackSearchOptionsBuilder.cs
  43. +2 1  MongoDB.Driver/Builders/GeoNearOptionsBuilder.cs
  44. +2 1  MongoDB.Driver/Builders/GroupByBuilder.cs
  45. +2 1  MongoDB.Driver/Builders/IndexKeysBuilder.cs
  46. +2 1  MongoDB.Driver/Builders/IndexOptionsBuilder.cs
  47. +3 2 MongoDB.Driver/Builders/MapReduceOptionsBuilder.cs
  48. +2 1  MongoDB.Driver/Builders/SortByBuilder.cs
  49. +2 2 MongoDB.Driver/Builders/UpdateBuilder.cs
  50. +3 2 MongoDB.Driver/Communication/Messages/MongoReplyMessage.cs
  51. +2 1  MongoDB.Driver/GridFS/MongoGridFSFileInfo.cs
  52. +2 2 MongoDB.Driver/MongoDBRef.cs
  53. +2 1  MongoDB.DriverUnitTests/Jira/CSharp231Tests.cs
  54. +1 1  MongoDB.DriverUnitTests/Jira/CSharp365Tests.cs
20 MongoDB.Bson/ObjectModel/BsonArray.cs
@@ -21,6 +21,7 @@
21 21 using System.Text.RegularExpressions;
22 22
23 23 using MongoDB.Bson.IO;
  24 +using MongoDB.Bson.Serialization;
24 25
25 26 namespace MongoDB.Bson
26 27 {
@@ -412,17 +413,10 @@ public new static BsonArray Create(object value)
412 413 /// </summary>
413 414 /// <param name="bsonReader">The reader.</param>
414 415 /// <returns>A BsonArray.</returns>
  416 + [Obsolete("Use BsonSerializer.Deserialize<BsonArray> instead.")]
415 417 public static new BsonArray ReadFrom(BsonReader bsonReader)
416 418 {
417   - var array = new BsonArray();
418   - bsonReader.ReadStartArray();
419   - while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
420   - {
421   - var value = BsonValue.ReadFrom(bsonReader);
422   - array.Add(value);
423   - }
424   - bsonReader.ReadEndArray();
425   - return array;
  419 + return BsonSerializer.Deserialize<BsonArray>(bsonReader);
426 420 }
427 421
428 422 // public methods
@@ -870,14 +864,10 @@ public override string ToString()
870 864 /// Writes the array to a BsonWriter.
871 865 /// </summary>
872 866 /// <param name="bsonWriter">The writer.</param>
  867 + [Obsolete("Use BsonSerializer.Serialize<BsonArray> instead.")]
873 868 public new void WriteTo(BsonWriter bsonWriter)
874 869 {
875   - bsonWriter.WriteStartArray();
876   - for (int i = 0; i < _values.Count; i++)
877   - {
878   - _values[i].WriteTo(bsonWriter);
879   - }
880   - bsonWriter.WriteEndArray();
  870 + BsonSerializer.Serialize(bsonWriter, this);
881 871 }
882 872
883 873 // explicit interface implementations
174 MongoDB.Bson/ObjectModel/BsonDocument.cs
@@ -26,6 +26,7 @@
26 26 using MongoDB.Bson.Serialization;
27 27 using MongoDB.Bson.Serialization.IdGenerators;
28 28 using MongoDB.Bson.Serialization.Options;
  29 +using MongoDB.Bson.Serialization.Serializers;
29 30
30 31 namespace MongoDB.Bson
31 32 {
@@ -371,8 +372,7 @@ public static BsonDocument Parse(string json)
371 372 {
372 373 using (var bsonReader = BsonReader.Create(json))
373 374 {
374   - var document = new BsonDocument();
375   - return (BsonDocument)((IBsonSerializable)document).Deserialize(bsonReader, typeof(BsonDocument), null);
  375 + return (BsonDocument)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), null);
376 376 }
377 377 }
378 378
@@ -381,11 +381,12 @@ public static BsonDocument Parse(string json)
381 381 /// </summary>
382 382 /// <param name="buffer">The BsonBuffer.</param>
383 383 /// <returns>A BsonDocument.</returns>
  384 + [Obsolete("Use BsonSerializer.Deserialize<BsonDocument> instead.")]
384 385 public static BsonDocument ReadFrom(BsonBuffer buffer)
385 386 {
386 387 using (BsonReader bsonReader = BsonReader.Create(buffer))
387 388 {
388   - return ReadFrom(bsonReader);
  389 + return BsonSerializer.Deserialize<BsonDocument>(bsonReader);
389 390 }
390 391 }
391 392
@@ -394,10 +395,10 @@ public static BsonDocument ReadFrom(BsonBuffer buffer)
394 395 /// </summary>
395 396 /// <param name="bsonReader">The BsonReader.</param>
396 397 /// <returns>A BsonDocument.</returns>
  398 + [Obsolete("Use BsonSerializer.Deserialize<BsonDocument> instead.")]
397 399 public static new BsonDocument ReadFrom(BsonReader bsonReader)
398 400 {
399   - BsonDocument document = new BsonDocument();
400   - return (BsonDocument)((IBsonSerializable)document).Deserialize(bsonReader, typeof(BsonDocument), null);
  401 + return BsonSerializer.Deserialize<BsonDocument>(bsonReader);
401 402 }
402 403
403 404 /// <summary>
@@ -405,13 +406,10 @@ public static new BsonDocument ReadFrom(BsonReader bsonReader)
405 406 /// </summary>
406 407 /// <param name="bytes">The byte array.</param>
407 408 /// <returns>A BsonDocument.</returns>
  409 + [Obsolete("Use BsonSerializer.Deserialize<BsonDocument> instead.")]
408 410 public static BsonDocument ReadFrom(byte[] bytes)
409 411 {
410   - MemoryStream stream = new MemoryStream(bytes);
411   - using (BsonReader bsonReader = BsonReader.Create(stream))
412   - {
413   - return ReadFrom(bsonReader);
414   - }
  412 + return BsonSerializer.Deserialize<BsonDocument>(bytes);
415 413 }
416 414
417 415 /// <summary>
@@ -419,12 +417,10 @@ public static BsonDocument ReadFrom(byte[] bytes)
419 417 /// </summary>
420 418 /// <param name="stream">The stream.</param>
421 419 /// <returns>A BsonDocument.</returns>
  420 + [Obsolete("Use BsonSerializer.Deserialize<BsonDocument> instead.")]
422 421 public static BsonDocument ReadFrom(Stream stream)
423 422 {
424   - using (BsonReader bsonReader = BsonReader.Create(stream))
425   - {
426   - return ReadFrom(bsonReader);
427   - }
  423 + return BsonSerializer.Deserialize<BsonDocument>(stream);
428 424 }
429 425
430 426 /// <summary>
@@ -432,12 +428,12 @@ public static BsonDocument ReadFrom(Stream stream)
432 428 /// </summary>
433 429 /// <param name="filename">The name of the file.</param>
434 430 /// <returns>A BsonDocument.</returns>
  431 + [Obsolete("Use BsonSerializer.Deserialize<BsonDocument> instead.")]
435 432 public static BsonDocument ReadFrom(string filename)
436 433 {
437   - FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
438   - using (BsonReader bsonReader = BsonReader.Create(stream))
  434 + using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None))
439 435 {
440   - return ReadFrom(bsonReader);
  436 + return BsonSerializer.Deserialize<BsonDocument>(stream);
441 437 }
442 438 }
443 439
@@ -796,34 +792,7 @@ public override BsonValue DeepClone()
796 792 [Obsolete("Deserialize was intended to be private and will become private in a future release.")]
797 793 public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
798 794 {
799   - if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
800   - {
801   - bsonReader.ReadNull();
802   - return null;
803   - }
804   - else
805   - {
806   - var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
807   - if (documentSerializationOptions == null)
808   - {
809   - var message = string.Format(
810   - "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
811   - BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
812   - BsonUtils.GetFriendlyTypeName(options.GetType()));
813   - throw new BsonSerializationException(message);
814   - }
815   - _allowDuplicateNames = documentSerializationOptions.AllowDuplicateNames;
816   -
817   - bsonReader.ReadStartDocument();
818   - Clear();
819   - BsonElement element;
820   - while (BsonElement.ReadFrom(bsonReader, out element))
821   - {
822   - Add(element);
823   - }
824   - bsonReader.ReadEndDocument();
825   - return this;
826   - }
  795 + return BsonDocumentSerializer.Instance.Deserialize(bsonReader, nominalType, options);
827 796 }
828 797
829 798 /// <summary>
@@ -836,37 +805,8 @@ public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializ
836 805 [Obsolete("GetDocumentId was intended to be private and will become private in a future release. Use document[\"_id\"] or document.GetValue(\"_id\") instead.")]
837 806 public bool GetDocumentId(out object id, out Type idNominalType, out IIdGenerator idGenerator)
838 807 {
839   - BsonElement idElement;
840   - if (TryGetElement("_id", out idElement))
841   - {
842   - // TODO: in a future release we will always return a BsonValue (GetDocumentId is not supposed to transform Id values in any way)
843   - // we're returning the raw value in 1.4.2 to remain temporarily backward compatible with 1.4.0 and earlier
844   - id = idElement.Value.RawValue;
845   - if (id == null)
846   - {
847   - id = idElement.Value;
848   - }
849   - idGenerator = BsonSerializer.LookupIdGenerator(id.GetType());
850   -
851   - if (idGenerator == null)
852   - {
853   - // note: in 1.4.2 this code isn't yet used because if the RawValue was a Guid then the GuidIdGenerator was found
854   - // but once we start returning BsonValue like we should this code will start being used
855   - var idBinaryData = id as BsonBinaryData;
856   - if (idBinaryData != null && (idBinaryData.SubType == BsonBinarySubType.UuidLegacy || idBinaryData.SubType == BsonBinarySubType.UuidStandard))
857   - {
858   - idGenerator = BsonBinaryDataGuidGenerator.GetInstance(idBinaryData.GuidRepresentation);
859   - }
860   - }
861   - }
862   - else
863   - {
864   - id = null;
865   - idGenerator = ObjectIdGenerator.Instance; // TODO: in a future release we will return an instance of BsonObjectIdGenerator
866   - }
867   -
868   - idNominalType = typeof(BsonValue);
869   - return true;
  808 + var idProvider = (IBsonIdProvider)BsonDocumentSerializer.Instance;
  809 + return idProvider.GetDocumentId(this, out id, out idNominalType, out idGenerator);
870 810 }
871 811
872 812 /// <summary>
@@ -1102,45 +1042,7 @@ public void RemoveElement(BsonElement element)
1102 1042 [Obsolete("Serialize was intended to be private and will become private in a future release.")]
1103 1043 public void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
1104 1044 {
1105   - if (bsonWriter == null)
1106   - {
1107   - throw new ArgumentNullException("bsonWriter");
1108   - }
1109   - if (nominalType == null)
1110   - {
1111   - throw new ArgumentNullException("nominalType");
1112   - }
1113   - var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
1114   - if (documentSerializationOptions == null)
1115   - {
1116   - var message = string.Format(
1117   - "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
1118   - BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
1119   - BsonUtils.GetFriendlyTypeName(options.GetType()));
1120   - throw new BsonSerializationException(message);
1121   - }
1122   -
1123   - bsonWriter.WriteStartDocument();
1124   - int idIndex;
1125   - if (documentSerializationOptions.SerializeIdFirst && _indexes.TryGetValue("_id", out idIndex))
1126   - {
1127   - _elements[idIndex].WriteTo(bsonWriter);
1128   - }
1129   - else
1130   - {
1131   - idIndex = -1; // remember that when TryGetValue returns false it sets idIndex to 0
1132   - }
1133   -
1134   - for (int i = 0; i < _elements.Count; i++)
1135   - {
1136   - // if serializeIdFirst is false then idIndex will be -1 and no elements will be skipped
1137   - if (i != idIndex)
1138   - {
1139   - _elements[i].WriteTo(bsonWriter);
1140   - }
1141   - }
1142   -
1143   - bsonWriter.WriteEndDocument();
  1045 + BsonDocumentSerializer.Instance.Serialize(bsonWriter, nominalType, this, options);
1144 1046 }
1145 1047
1146 1048 /// <summary>
@@ -1186,31 +1088,8 @@ public BsonDocument Set(string name, BsonValue value)
1186 1088 [Obsolete("SetDocumentId was intended to be private and will become private in a future release. Use document[\"_id\"] = value or document.Set(\"_id\", value) instead.")]
1187 1089 public void SetDocumentId(object id)
1188 1090 {
1189   - if (id == null)
1190   - {
1191   - throw new ArgumentNullException("id");
1192   - }
1193   -
1194   - // TODO: in a future release we will just cast directly to BsonValue because the caller is required to pass a BsonValue
1195   - // var idBsonValue = (BsonValue)id;
1196   -
1197   - var idBsonValue = id as BsonValue;
1198   - if (idBsonValue == null)
1199   - {
1200   - // SetDocumentId contract requires that caller provide an id of the correct type so this conversion is not correct
1201   - // for temporary backward compatibility with versions prior to 1.4.2 we continue to map objects to BsonValue for now
1202   - idBsonValue = BsonValue.Create(id); // TODO: in a future release this mapping will be removed
1203   - }
1204   -
1205   - BsonElement idElement;
1206   - if (TryGetElement("_id", out idElement))
1207   - {
1208   - idElement.Value = idBsonValue;
1209   - }
1210   - else
1211   - {
1212   - InsertAt(0, new BsonElement("_id", idBsonValue));
1213   - }
  1091 + var idProvider = (IBsonIdProvider)BsonDocumentSerializer.Instance;
  1092 + idProvider.SetDocumentId(this, id);
1214 1093 }
1215 1094
1216 1095 /// <summary>
@@ -1348,20 +1227,22 @@ public bool TryGetValue(string name, out BsonValue value)
1348 1227 /// Writes the document to a BsonWriter.
1349 1228 /// </summary>
1350 1229 /// <param name="bsonWriter">The writer.</param>
  1230 + [Obsolete("Use BsonSerializer.Serialize<BsonDocument> instead.")]
1351 1231 public new void WriteTo(BsonWriter bsonWriter)
1352 1232 {
1353   - ((IBsonSerializable)this).Serialize(bsonWriter, typeof(BsonDocument), null);
  1233 + BsonSerializer.Serialize(bsonWriter, this);
1354 1234 }
1355 1235
1356 1236 /// <summary>
1357 1237 /// Writes the document to a BsonBuffer.
1358 1238 /// </summary>
1359 1239 /// <param name="buffer">The buffer.</param>
  1240 + [Obsolete("Use BsonSerializer.Serialize<BsonDocument> instead.")]
1360 1241 public void WriteTo(BsonBuffer buffer)
1361 1242 {
1362 1243 using (BsonWriter bsonWriter = BsonWriter.Create(buffer))
1363 1244 {
1364   - WriteTo(bsonWriter);
  1245 + BsonSerializer.Serialize(bsonWriter, this);
1365 1246 }
1366 1247 }
1367 1248
@@ -1369,11 +1250,12 @@ public void WriteTo(BsonBuffer buffer)
1369 1250 /// Writes the document to a Stream.
1370 1251 /// </summary>
1371 1252 /// <param name="stream">The stream.</param>
  1253 + [Obsolete("Use BsonSerializer.Serialize<BsonDocument> instead.")]
1372 1254 public void WriteTo(Stream stream)
1373 1255 {
1374 1256 using (BsonWriter bsonWriter = BsonWriter.Create(stream))
1375 1257 {
1376   - WriteTo(bsonWriter);
  1258 + BsonSerializer.Serialize(bsonWriter, this);
1377 1259 }
1378 1260 }
1379 1261
@@ -1381,11 +1263,15 @@ public void WriteTo(Stream stream)
1381 1263 /// Writes the document to a file.
1382 1264 /// </summary>
1383 1265 /// <param name="filename">The name of the file.</param>
  1266 + [Obsolete("Use BsonSerializer.Serialize<BsonDocument> instead.")]
1384 1267 public void WriteTo(string filename)
1385 1268 {
1386 1269 using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
1387 1270 {
1388   - WriteTo(stream);
  1271 + using (BsonWriter bsonWriter = BsonWriter.Create(stream))
  1272 + {
  1273 + BsonSerializer.Serialize(bsonWriter, this);
  1274 + }
1389 1275 }
1390 1276 }
1391 1277
47 MongoDB.Bson/ObjectModel/BsonDocumentWrapper.cs
@@ -24,6 +24,7 @@
24 24
25 25 using MongoDB.Bson.IO;
26 26 using MongoDB.Bson.Serialization;
  27 +using MongoDB.Bson.Serialization.Serializers;
27 28
28 29 namespace MongoDB.Bson
29 30 {
@@ -86,6 +87,31 @@ public BsonDocumentWrapper(Type wrappedNominalType, object wrappedObject, bool i
86 87 _isUpdateDocument = isUpdateDocument;
87 88 }
88 89
  90 + // public properties
  91 + /// <summary>
  92 + /// Gets whether the wrapped document is an update document.
  93 + /// </summary>
  94 + public bool IsUpdateDocument
  95 + {
  96 + get { return _isUpdateDocument; }
  97 + }
  98 +
  99 + /// <summary>
  100 + /// Gets the nominal type of the wrapped document.
  101 + /// </summary>
  102 + public Type WrappedNominalType
  103 + {
  104 + get { return _wrappedNominalType; }
  105 + }
  106 +
  107 + /// <summary>
  108 + /// Gets the wrapped object.
  109 + /// </summary>
  110 + public object WrappedObject
  111 + {
  112 + get { return _wrappedObject; }
  113 + }
  114 +
89 115 // public static methods
90 116 /// <summary>
91 117 /// Creates a new instance of the BsonDocumentWrapper class.
@@ -234,26 +260,7 @@ public override int GetHashCode()
234 260 [Obsolete("Serialize was intended to be private and will become private in a future release.")]
235 261 public void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
236 262 {
237   - if (_isUpdateDocument)
238   - {
239   - var savedCheckElementNames = bsonWriter.CheckElementNames;
240   - var savedCheckUpdateDocument = bsonWriter.CheckUpdateDocument;
241   - try
242   - {
243   - bsonWriter.CheckElementNames = false;
244   - bsonWriter.CheckUpdateDocument = true;
245   - BsonSerializer.Serialize(bsonWriter, _wrappedNominalType, _wrappedObject, options);
246   - }
247   - finally
248   - {
249   - bsonWriter.CheckElementNames = savedCheckElementNames;
250   - bsonWriter.CheckUpdateDocument = savedCheckUpdateDocument;
251   - }
252   - }
253   - else
254   - {
255   - BsonSerializer.Serialize(bsonWriter, _wrappedNominalType, _wrappedObject, options);
256   - }
  263 + BsonDocumentWrapperSerializer.Instance.Serialize(bsonWriter, nominalType, this, options);
257 264 }
258 265
259 266 /// <summary>
44 MongoDB.Bson/ObjectModel/BsonElement.cs
@@ -159,43 +159,6 @@ public static BsonElement Create(string name, BsonValue value)
159 159 }
160 160 }
161 161
162   - // internal static methods
163   - internal static bool ReadFrom(BsonReader bsonReader, out BsonElement element)
164   - {
165   - BsonType bsonType;
166   - if ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
167   - {
168   - var name = bsonReader.ReadName();
169   - var value = BsonValue.ReadFrom(bsonReader);
170   - element = new BsonElement(name, value);
171   - return true;
172   - }
173   - else
174   - {
175   - element = null;
176   - return false;
177   - }
178   - }
179   -
180   - internal static BsonElement ReadFrom(BsonReader bsonReader, string expectedName)
181   - {
182   - BsonElement element;
183   - if (ReadFrom(bsonReader, out element))
184   - {
185   - if (element.Name != expectedName)
186   - {
187   - string message = string.Format("Expected element '{0}', not '{1}'.", expectedName, element._name);
188   - throw new FileFormatException(message);
189   - }
190   - return element;
191   - }
192   - else
193   - {
194   - string message = string.Format("Element '{0}' is missing.", expectedName);
195   - throw new FileFormatException(message);
196   - }
197   - }
198   -
199 162 // private static methods
200 163 private static void ValidateElementName(string name)
201 164 {
@@ -285,12 +248,5 @@ public override string ToString()
285 248 {
286 249 return string.Format("{0}={1}", _name, _value);
287 250 }
288   -
289   - // internal methods
290   - internal void WriteTo(BsonWriter bsonWriter)
291   - {
292   - bsonWriter.WriteName(_name);
293   - _value.WriteTo(bsonWriter);
294   - }
295 251 }
296 252 }
157 MongoDB.Bson/ObjectModel/BsonValue.cs
@@ -1111,59 +1111,10 @@ public static BsonValue Create(object value)
1111 1111 /// </summary>
1112 1112 /// <param name="bsonReader">The reader.</param>
1113 1113 /// <returns>A BsonValue.</returns>
  1114 + [Obsolete("Use BsonSerializer.Deserialize<BsonValue> instead.")]
1114 1115 public static BsonValue ReadFrom(BsonReader bsonReader)
1115 1116 {
1116   - BsonType bsonType = bsonReader.GetCurrentBsonType();
1117   - switch (bsonType)
1118   - {
1119   - case BsonType.Array:
1120   - return BsonArray.ReadFrom(bsonReader);
1121   - case BsonType.Binary:
1122   - return bsonReader.ReadBinaryData();
1123   - case BsonType.Boolean:
1124   - return (BsonBoolean)bsonReader.ReadBoolean();
1125   - case BsonType.DateTime:
1126   - return new BsonDateTime(bsonReader.ReadDateTime());
1127   - case BsonType.Document:
1128   - return BsonDocument.ReadFrom(bsonReader);
1129   - case BsonType.Double:
1130   - return new BsonDouble(bsonReader.ReadDouble());
1131   - case BsonType.Int32:
1132   - return new BsonInt32(bsonReader.ReadInt32());
1133   - case BsonType.Int64:
1134   - return new BsonInt64(bsonReader.ReadInt64());
1135   - case BsonType.JavaScript:
1136   - return new BsonJavaScript(bsonReader.ReadJavaScript());
1137   - case BsonType.JavaScriptWithScope:
1138   - string code = bsonReader.ReadJavaScriptWithScope();
1139   - var scope = BsonDocument.ReadFrom(bsonReader);
1140   - return new BsonJavaScriptWithScope(code, scope);
1141   - case BsonType.MaxKey:
1142   - bsonReader.ReadMaxKey();
1143   - return BsonMaxKey.Value;
1144   - case BsonType.MinKey:
1145   - bsonReader.ReadMinKey();
1146   - return BsonMinKey.Value;
1147   - case BsonType.Null:
1148   - bsonReader.ReadNull();
1149   - return BsonNull.Value;
1150   - case BsonType.ObjectId:
1151   - return bsonReader.ReadObjectId();
1152   - case BsonType.RegularExpression:
1153   - return bsonReader.ReadRegularExpression();
1154   - case BsonType.String:
1155   - return new BsonString(bsonReader.ReadString());
1156   - case BsonType.Symbol:
1157   - return BsonSymbolTable.Lookup(bsonReader.ReadSymbol());
1158   - case BsonType.Timestamp:
1159   - return new BsonTimestamp(bsonReader.ReadTimestamp());
1160   - case BsonType.Undefined:
1161   - bsonReader.ReadUndefined();
1162   - return BsonUndefined.Value;
1163   - default:
1164   - var message = string.Format("Invalid BsonType {0}.", bsonType);
1165   - throw new BsonInternalException(message);
1166   - }
  1117 + return BsonSerializer.Deserialize<BsonValue>(bsonReader);
1167 1118 }
1168 1119
1169 1120 // public methods
@@ -1316,110 +1267,10 @@ public virtual DateTime ToUniversalTime()
1316 1267 /// Writes the BsonValue to a BsonWriter.
1317 1268 /// </summary>
1318 1269 /// <param name="bsonWriter">The writer.</param>
  1270 + [Obsolete("Use BsonSerializer.Serialize<BsonValue> instead.")]
1319 1271 public void WriteTo(BsonWriter bsonWriter)
1320 1272 {
1321   - switch (_bsonType)
1322   - {
1323   - case BsonType.Array:
1324   - ((BsonArray)this).WriteTo(bsonWriter);
1325   - break;
1326   - case BsonType.Binary:
1327   - var binaryData = (BsonBinaryData)this;
1328   - var bytes = binaryData.Bytes;
1329   - var subType = binaryData.SubType;
1330   - var guidRepresentation = binaryData.GuidRepresentation;
1331   - var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation;
1332   - if (subType == BsonBinarySubType.UuidLegacy && writerGuidRepresentation != GuidRepresentation.Unspecified)
1333   - {
1334   - if (guidRepresentation != writerGuidRepresentation)
1335   - {
1336   - if (guidRepresentation == GuidRepresentation.Unspecified)
1337   - {
1338   - var message = string.Format("Cannot write binary data of sub type UuidLegacy and GuidRepresentation Unspecified to a collection with GuidRepresentation {0}.", writerGuidRepresentation);
1339   - throw new BsonSerializationException(message);
1340   - }
1341   - var guid = GuidConverter.FromBytes(bytes, guidRepresentation);
1342   - bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation);
1343   - subType = (writerGuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
1344   - guidRepresentation = writerGuidRepresentation;
1345   - }
1346   - }
1347   - binaryData = new BsonBinaryData(bytes, subType, guidRepresentation);
1348   - bsonWriter.WriteBinaryData(binaryData);
1349   - break;
1350   - case BsonType.Boolean:
1351   - bsonWriter.WriteBoolean(((BsonBoolean)this).Value);
1352   - break;
1353   - case BsonType.DateTime:
1354   - bsonWriter.WriteDateTime(((BsonDateTime)this).MillisecondsSinceEpoch);
1355   - break;
1356   - case BsonType.Document:
1357   - var document = this as BsonDocument;
1358   - if (document != null)
1359   - {
1360   - document.WriteTo(bsonWriter);
1361   - }
1362   - else
1363   - {
1364   - var documentWrapper = this as BsonDocumentWrapper;
1365   - if (documentWrapper != null)
1366   - {
1367   - ((IBsonSerializable)documentWrapper).Serialize(bsonWriter, typeof(BsonDocument), null);
1368   - }
1369   - else
1370   - {
1371   - var message = string.Format("BsonType Document can only be used with the classes BsonDocument or BsonDocumentWrapper, not with the class {0}.", this.GetType().FullName);
1372   - throw new BsonInternalException(message);
1373   - }
1374   - }
1375   - break;
1376   - case BsonType.Double:
1377   - bsonWriter.WriteDouble(((BsonDouble)this).Value);
1378   - break;
1379   - case BsonType.Int32:
1380   - bsonWriter.WriteInt32(((BsonInt32)this).Value);
1381   - break;
1382   - case BsonType.Int64:
1383   - bsonWriter.WriteInt64(((BsonInt64)this).Value);
1384   - break;
1385   - case BsonType.JavaScript:
1386   - bsonWriter.WriteJavaScript(((BsonJavaScript)this).Code);
1387   - break;
1388   - case BsonType.JavaScriptWithScope:
1389   - var script = (BsonJavaScriptWithScope)this;
1390   - bsonWriter.WriteJavaScriptWithScope(script.Code);
1391   - script.Scope.WriteTo(bsonWriter);
1392   - break;
1393   - case BsonType.MaxKey:
1394   - bsonWriter.WriteMaxKey();
1395   - break;
1396   - case BsonType.MinKey:
1397   - bsonWriter.WriteMinKey();
1398   - break;
1399   - case BsonType.Null:
1400   - bsonWriter.WriteNull();
1401   - break;
1402   - case BsonType.ObjectId:
1403   - var objectId = ((BsonObjectId)this).Value;
1404   - bsonWriter.WriteObjectId(objectId);
1405   - break;
1406   - case BsonType.RegularExpression:
1407   - BsonRegularExpression regex = (BsonRegularExpression)this;
1408   - bsonWriter.WriteRegularExpression(regex);
1409   - break;
1410   - case BsonType.String:
1411   - bsonWriter.WriteString(((BsonString)this).Value);
1412   - break;
1413   - case BsonType.Symbol:
1414   - bsonWriter.WriteSymbol(((BsonSymbol)this).Name);
1415   - break;
1416   - case BsonType.Timestamp:
1417   - bsonWriter.WriteTimestamp(((BsonTimestamp)this).Value);
1418   - break;
1419   - case BsonType.Undefined:
1420   - bsonWriter.WriteUndefined();
1421   - break;
1422   - }
  1273 + BsonSerializer.Serialize(bsonWriter, this);
1423 1274 }
1424 1275
1425 1276 // protected methods
5 MongoDB.Bson/Serialization/BsonDefaultSerializationProvider.cs
@@ -128,6 +128,11 @@ public IBsonSerializer GetSerializer(Type type)
128 128 return serializer;
129 129 }
130 130
  131 + if (typeof(BsonDocument).IsAssignableFrom(type))
  132 + {
  133 + return BsonDocumentSerializer.Instance;
  134 + }
  135 +
131 136 if (type.IsGenericType)
132 137 {
133 138 var genericTypeDefinition = type.GetGenericTypeDefinition();
10 MongoDB.Bson/Serialization/BsonMemberMap.cs
@@ -40,6 +40,7 @@ public class BsonMemberMap
40 40 private int _order = int.MaxValue;
41 41 private MemberInfo _memberInfo;
42 42 private Type _memberType;
  43 + private bool _memberTypeIsBsonValue;
43 44 private Func<object, object> _getter;
44 45 private Action<object, object> _setter;
45 46 private IBsonSerializationOptions _serializationOptions;
@@ -65,6 +66,7 @@ public BsonMemberMap(BsonClassMap classMap, MemberInfo memberInfo)
65 66 _classMap = classMap;
66 67 _memberInfo = memberInfo;
67 68 _memberType = BsonClassMap.GetMemberInfoType(memberInfo);
  69 + _memberTypeIsBsonValue = typeof(BsonValue).IsAssignableFrom(_memberType);
68 70 _defaultValue = GetDefaultValue(_memberType);
69 71 _defaultValueSpecified = false;
70 72 }
@@ -95,6 +97,14 @@ public Type MemberType
95 97 }
96 98
97 99 /// <summary>
  100 + /// Gets whether the member type is a BsonValue.
  101 + /// </summary>
  102 + public bool MemberTypeIsBsonValue
  103 + {
  104 + get { return _memberTypeIsBsonValue; }
  105 + }
  106 +
  107 + /// <summary>
98 108 /// Gets the name of the element.
99 109 /// </summary>
100 110 public string ElementName
24 MongoDB.Bson/Serialization/BsonSerializer.cs
@@ -25,6 +25,7 @@
25 25 using MongoDB.Bson.Serialization.Attributes;
26 26 using MongoDB.Bson.Serialization.Conventions;
27 27 using MongoDB.Bson.Serialization.IdGenerators;
  28 +using MongoDB.Bson.Serialization.Serializers;
28 29
29 30 namespace MongoDB.Bson.Serialization
30 31 {
@@ -207,8 +208,7 @@ public static object Deserialize(BsonReader bsonReader, Type nominalType, IBsonS
207 208 {
208 209 if (nominalType == typeof(BsonDocument))
209 210 {
210   - var bsonDocument = new BsonDocument();
211   - return ((IBsonSerializable)bsonDocument).Deserialize(bsonReader, nominalType, options);
  211 + return BsonDocumentSerializer.Instance.Deserialize(bsonReader, nominalType, options);
212 212 }
213 213
214 214 // if nominalType is an interface find out the actualType and use it instead
@@ -529,6 +529,12 @@ public static IIdGenerator LookupIdGenerator(Type type)
529 529 /// <returns>A serializer for the Type.</returns>
530 530 public static IBsonSerializer LookupSerializer(Type type)
531 531 {
  532 + // since we don't allow registering serializers for BsonDocument no lookup is needed
  533 + if (type == typeof(BsonDocument))
  534 + {
  535 + return BsonDocumentSerializer.Instance;
  536 + }
  537 +
532 538 // since we don't allow registering serializers for classes that implement IBsonSerializable no lookup is needed
533 539 if (typeof(IBsonSerializable).IsAssignableFrom(type))
534 540 {
@@ -734,6 +740,13 @@ public static void RegisterSerializationProvider(IBsonSerializationProvider prov
734 740 /// <param name="serializer">The serializer.</param>
735 741 public static void RegisterSerializer(Type type, IBsonSerializer serializer)
736 742 {
  743 + // don't allow any serializers to be registered for subclasses of BsonDocument
  744 + if (typeof(BsonDocument).IsAssignableFrom(type))
  745 + {
  746 + var message = string.Format("A serializer cannot be registered for type {0} because it is a subclass of BsonDocument.", BsonUtils.GetFriendlyTypeName(type));
  747 + throw new BsonSerializationException(message);
  748 + }
  749 +
737 750 if (typeof(IBsonSerializable).IsAssignableFrom(type))
738 751 {
739 752 var message = string.Format("A serializer cannot be registered for type {0} because it implements IBsonSerializable.", BsonUtils.GetFriendlyTypeName(type));
@@ -806,6 +819,13 @@ public static void Serialize(BsonWriter bsonWriter, Type nominalType, object val
806 819 object value,
807 820 IBsonSerializationOptions options)
808 821 {
  822 + // since we don't allow registering serializers for BsonDocument no lookup is needed
  823 + if (nominalType == typeof(BsonDocument))
  824 + {
  825 + BsonDocumentSerializer.Instance.Serialize(bsonWriter, nominalType, value, options);
  826 + return;
  827 + }
  828 +
809 829 // since we don't allow registering serializers for classes that implement IBsonSerializable no lookup is needed
810 830 var bsonSerializable = value as IBsonSerializable;
811 831 if (bsonSerializable != null)
3  MongoDB.Bson/Serialization/Conventions/DiscriminatorConventions.cs
@@ -20,6 +20,7 @@
20 20 using System.Reflection;
21 21
22 22 using MongoDB.Bson.IO;
  23 +using MongoDB.Bson.Serialization.Serializers;
23 24
24 25 namespace MongoDB.Bson.Serialization.Conventions
25 26 {
@@ -153,7 +154,7 @@ public Type GetActualType(BsonReader bsonReader, Type nominalType)
153 154 var actualType = nominalType;
154 155 if (bsonReader.FindElement(_elementName))
155 156 {
156   - var discriminator = BsonValue.ReadFrom(bsonReader);
  157 + var discriminator = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
157 158 if (discriminator.IsBsonArray)
158 159 {
159 160 discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
35 MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs
@@ -15,11 +15,11 @@
15 15
16 16 using System;
17 17 using System.Collections.Generic;
  18 +using System.IO;
18 19 using System.Linq;
19 20 using System.Text;
20 21
21 22 using MongoDB.Bson.IO;
22   -using MongoDB.Bson.Serialization.Options;
23 23
24 24 namespace MongoDB.Bson.Serialization.Serializers
25 25 {
@@ -36,7 +36,6 @@ public class BsonArraySerializer : BsonBaseSerializer
36 36 /// Initializes a new instance of the BsonArraySerializer class.
37 37 /// </summary>
38 38 public BsonArraySerializer()
39   - : base(new RepresentationSerializationOptions(BsonType.Array))
40 39 {
41 40 }
42 41
@@ -67,14 +66,21 @@ public static BsonArraySerializer Instance
67 66 VerifyTypes(nominalType, actualType, typeof(BsonArray));
68 67
69 68 var bsonType = bsonReader.GetCurrentBsonType();
70   - if (bsonType == BsonType.Null)
  69 + switch (bsonType)
71 70 {
72   - bsonReader.ReadNull();
73   - return null;
74   - }
75   - else
76   - {
77   - return BsonArray.ReadFrom(bsonReader);
  71 + case BsonType.Array:
  72 + bsonReader.ReadStartArray();
  73 + var array = new BsonArray();
  74 + while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  75 + {
  76 + var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
  77 + array.Add(value);
  78 + }
  79 + bsonReader.ReadEndArray();
  80 + return array;
  81 + default:
  82 + var message = string.Format("Cannot deserialize BsonArray from BsonType {0}.", bsonType);
  83 + throw new FileFormatException(message);
78 84 }
79 85 }
80 86
@@ -93,13 +99,16 @@ public static BsonArraySerializer Instance
93 99 {
94 100 if (value == null)
95 101 {
96   - bsonWriter.WriteNull();
  102 + throw new ArgumentNullException("value");
97 103 }
98   - else
  104 +
  105 + var array = (BsonArray)value;
  106 + bsonWriter.WriteStartArray();
  107 + for (int i = 0; i < array.Count; i++)
99 108 {
100   - var array = (BsonArray)value;
101   - array.WriteTo(bsonWriter);
  109 + BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), array[i], null);
102 110 }
  111 + bsonWriter.WriteEndArray();
103 112 }
104 113 }
105 114 }
58 MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs
@@ -20,7 +20,6 @@
20 20 using System.Text;
21 21
22 22 using MongoDB.Bson.IO;
23   -using MongoDB.Bson.Serialization.Options;
24 23
25 24 namespace MongoDB.Bson.Serialization.Serializers
26 25 {
@@ -37,7 +36,6 @@ public class BsonBinaryDataSerializer : BsonBaseSerializer
37 36 /// Initializes a new instance of the BsonBinaryDataSerializer class.
38 37 /// </summary>
39 38 public BsonBinaryDataSerializer()
40   - : base(new RepresentationSerializationOptions(BsonType.Binary))
41 39 {
42 40 }
43 41
@@ -70,9 +68,6 @@ public static BsonBinaryDataSerializer Instance
70 68 var bsonType = bsonReader.GetCurrentBsonType();
71 69 switch (bsonType)
72 70 {
73   - case BsonType.Null:
74   - bsonReader.ReadNull();
75   - return null;
76 71 case BsonType.Binary:
77 72 return bsonReader.ReadBinaryData();
78 73 default:
@@ -96,40 +91,39 @@ public static BsonBinaryDataSerializer Instance
96 91 {
97 92 if (value == null)
98 93 {
99   - bsonWriter.WriteNull();
  94 + throw new ArgumentNullException("value");
100 95 }
101   - else
102   - {
103   - var binaryData = (BsonBinaryData)value;
104   - var bytes = binaryData.Bytes;
105   - var subType = binaryData.SubType;
106   - var guidRepresentation = binaryData.GuidRepresentation;
107 96
108   - if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
  97 + var binaryData = (BsonBinaryData)value;
  98 +
  99 + var subType = binaryData.SubType;
  100 + if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
  101 + {
  102 + var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation;
  103 + if (writerGuidRepresentation != GuidRepresentation.Unspecified)
109 104 {
110   - var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation;
111   - if (writerGuidRepresentation != GuidRepresentation.Unspecified)
  105 + var bytes = binaryData.Bytes;
  106 + var guidRepresentation = binaryData.GuidRepresentation;
  107 +
  108 + if (guidRepresentation == GuidRepresentation.Unspecified)
112 109 {
113   - if (guidRepresentation == GuidRepresentation.Unspecified)
114   - {
115   - var message = string.Format(
116   - "Cannot serialize BsonBinaryData with GuidRepresentation Unspecified to destination with GuidRepresentation {0}.",
117   - writerGuidRepresentation);
118   - throw new BsonSerializationException(message);
119   - }
120   - if (guidRepresentation != writerGuidRepresentation)
121   - {
122   - var guid = GuidConverter.FromBytes(bytes, guidRepresentation);
123   - bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation);
124   - subType = (writerGuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
125   - guidRepresentation = writerGuidRepresentation;
126   - }
  110 + var message = string.Format(
  111 + "Cannot serialize BsonBinaryData with GuidRepresentation Unspecified to destination with GuidRepresentation {0}.",
  112 + writerGuidRepresentation);
  113 + throw new BsonSerializationException(message);
  114 + }
  115 + if (guidRepresentation != writerGuidRepresentation)
  116 + {
  117 + var guid = GuidConverter.FromBytes(bytes, guidRepresentation);
  118 + bytes = GuidConverter.ToBytes(guid, writerGuidRepresentation);
  119 + subType = (writerGuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy;
  120 + guidRepresentation = writerGuidRepresentation;
  121 + binaryData = new BsonBinaryData(bytes, subType, guidRepresentation);
127 122 }
128 123 }
129   -
130   - binaryData = new BsonBinaryData(bytes, subType, guidRepresentation);
131   - bsonWriter.WriteBinaryData(binaryData);
132 124 }
  125 +
  126 + bsonWriter.WriteBinaryData(binaryData);
133 127 }
134 128 }
135 129 }
25 MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs
@@ -20,7 +20,6 @@
20 20 using System.Text;
21 21
22 22 using MongoDB.Bson.IO;
23   -using MongoDB.Bson.Serialization.Options;
24 23
25 24 namespace MongoDB.Bson.Serialization.Serializers
26 25 {
@@ -37,7 +36,6 @@ public class BsonBooleanSerializer : BsonBaseSerializer
37 36 /// Initializes a new instance of the BsonBooleanSerializer class.
38 37 /// </summary>
39 38 public BsonBooleanSerializer()
40   - : base(new RepresentationSerializationOptions(BsonType.Boolean))
41 39 {
42 40 }
43 41
@@ -68,14 +66,13 @@ public static BsonBooleanSerializer Instance
68 66 VerifyTypes(nominalType, actualType, typeof(BsonBoolean));
69 67
70 68 var bsonType = bsonReader.GetCurrentBsonType();
71   - if (bsonType == BsonType.Null)
  69 + switch (bsonType)
72 70 {
73   - bsonReader.ReadNull();
74   - return null;
75   - }
76   - else
77   - {
78   - return (BsonBoolean)((bool)BooleanSerializer.Instance.Deserialize(bsonReader, typeof(bool), options));
  71 + case BsonType.Boolean:
  72 + return (BsonBoolean)bsonReader.ReadBoolean();
  73 + default:
  74 + var message = string.Format("Cannot deserialize BsonBoolean from BsonType {0}.", bsonType);
  75 + throw new FileFormatException(message);
79 76 }
80 77 }
81 78
@@ -94,13 +91,11 @@ public static BsonBooleanSerializer Instance
94 91 {
95 92 if (value == null)
96 93 {
97   - bsonWriter.WriteNull();
98   - }
99   - else
100   - {
101   - var bsonBoolean = (BsonBoolean)value;
102   - BooleanSerializer.Instance.Serialize(bsonWriter, nominalType, bsonBoolean.Value, options);
  94 + throw new ArgumentNullException("value");
103 95 }
  96 +
  97 + var bsonBoolean = (BsonBoolean)value;
  98 + bsonWriter.WriteBoolean(bsonBoolean.Value);
104 99 }
105 100 }
106 101 }
85 MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs
@@ -24,6 +24,7 @@
24 24
25 25 using MongoDB.Bson.IO;
26 26 using MongoDB.Bson.Serialization.Options;
  27 +using MongoDB.Bson.Serialization.Serializers;
27 28
28 29 namespace MongoDB.Bson.Serialization
29 30 {
@@ -377,7 +378,7 @@ public BsonSerializationInfo GetMemberSerializationInfo(string memberName)
377 378 if (discriminator != null)
378 379 {
379 380 bsonWriter.WriteName(discriminatorConvention.ElementName);
380   - discriminator.WriteTo(bsonWriter);
  381 + BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), discriminator, null);
381 382 }
382 383 }
383 384 }
@@ -446,7 +447,7 @@ public void SetDocumentId(object document, object id)
446 447 extraElements = new BsonDocument();
447 448 extraElementsMemberMap.Setter(obj, extraElements);
448 449 }
449   - var bsonValue = BsonValue.ReadFrom(bsonReader);
  450 + var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
450 451 extraElements[elementName] = bsonValue;
451 452 }
452 453 else
@@ -464,7 +465,7 @@ public void SetDocumentId(object document, object id)
464 465 }
465 466 extraElementsMemberMap.Setter(obj, extraElements);
466 467 }
467   - var bsonValue = BsonValue.ReadFrom(bsonReader);
  468 + var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
468 469 extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
469 470 }
470 471 }
@@ -473,30 +474,45 @@ private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap
473 474 {