Skip to content

Commit 89db3dd

Browse files
author
Robert Stam
committed
Initial work on CSHARP-415 and CSHARP-417. BsonTypeMapper is now bidirectional with the introduction of the new MapToDotNetValue method. More careful analysis of when C# null means to ignore an item (as in functional construction) and when C# null maps to BsonNull.Value. Also looked carefully at when values cross the .NET to BsonDocument object model boundary and invoke MapToBsonValue or MapToDotNetValue as appropriate. BsonArray and BsonDocument now throw an exception on any attempt to set a value to C# null. Simplified GetDocumentId in BsonDocument to return the BsonValue unchanged. Added new BsonBinaryDataGuidGenerator for use with BsonDocument when the _id is a BsonBinaryData value holding a Guid. Changed BsonClassMapSerializer to use the new MapToDotNetValue method in the BsonTypeMapper instead of its own (now removed) version.
1 parent 08224bc commit 89db3dd

24 files changed

+761
-192
lines changed

Bson/Bson.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="ObjectModel\GuidConverter.cs" />
9292
<Compile Include="IO\JsonReaderSettings.cs" />
9393
<Compile Include="ObjectModel\ICustomBsonTypeMapper.cs" />
94+
<Compile Include="ObjectModel\BsonTypeMapperOptions.cs" />
9495
<Compile Include="Serialization\Attributes\BsonDateTimeOptionsAttribute.cs" />
9596
<Compile Include="Serialization\Attributes\BsonDictionaryOptionsAttribute.cs" />
9697
<Compile Include="Serialization\Attributes\BsonExtraElementsAttribute.cs" />

Bson/ObjectModel/BsonArray.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ public IEnumerable<BsonValue> Values
217217
public BsonValue this[int index]
218218
{
219219
get { return _values[index]; }
220-
set { _values[index] = value; }
220+
set {
221+
if (value == null)
222+
{
223+
throw new ArgumentNullException("value");
224+
}
225+
_values[index] = value;
226+
}
221227
}
222228

223229
// public static methods
@@ -450,7 +456,13 @@ public BsonArray AddRange(IEnumerable<BsonValue> values)
450456
{
451457
if (values != null)
452458
{
453-
_values.AddRange(values);
459+
foreach (var value in values)
460+
{
461+
if (value != null)
462+
{
463+
_values.Add(value);
464+
}
465+
}
454466
}
455467
return this;
456468
}
@@ -551,7 +563,7 @@ public BsonArray AddRange(IEnumerable<string> values)
551563
{
552564
foreach (var value in values)
553565
{
554-
_values.Add(BsonString.Create(value));
566+
_values.Add((value == null) ? (BsonValue)BsonNull.Value : BsonString.Create(value));
555567
}
556568
}
557569
return this;
@@ -568,7 +580,7 @@ public BsonArray AddRange(IEnumerable values)
568580
{
569581
foreach (var value in values)
570582
{
571-
_values.Add(BsonValue.Create(value));
583+
_values.Add(BsonTypeMapper.MapToBsonValue(value));
572584
}
573585
}
574586
return this;
@@ -635,6 +647,10 @@ public override int CompareTo(BsonValue other)
635647
/// <returns>True if the array contains the value.</returns>
636648
public bool Contains(BsonValue value)
637649
{
650+
if (value == null)
651+
{
652+
throw new ArgumentNullException("value");
653+
}
638654
return _values.Contains(value);
639655
}
640656

@@ -731,6 +747,10 @@ public override int GetHashCode()
731747
/// <returns>The zero based index of the value (or -1 if not found).</returns>
732748
public int IndexOf(BsonValue value)
733749
{
750+
if (value == null)
751+
{
752+
throw new ArgumentNullException("value");
753+
}
734754
return _values.IndexOf(value);
735755
}
736756

@@ -742,6 +762,10 @@ public int IndexOf(BsonValue value)
742762
/// <returns>The zero based index of the value (or -1 if not found).</returns>
743763
public int IndexOf(BsonValue value, int index)
744764
{
765+
if (value == null)
766+
{
767+
throw new ArgumentNullException("value");
768+
}
745769
return _values.IndexOf(value, index);
746770
}
747771

@@ -754,6 +778,10 @@ public int IndexOf(BsonValue value, int index)
754778
/// <returns>The zero based index of the value (or -1 if not found).</returns>
755779
public int IndexOf(BsonValue value, int index, int count)
756780
{
781+
if (value == null)
782+
{
783+
throw new ArgumentNullException("value");
784+
}
757785
return _values.IndexOf(value, index, count);
758786
}
759787

@@ -764,6 +792,10 @@ public int IndexOf(BsonValue value, int index, int count)
764792
/// <param name="value">The new value.</param>
765793
public void Insert(int index, BsonValue value)
766794
{
795+
if (value == null)
796+
{
797+
throw new ArgumentNullException("value");
798+
}
767799
_values.Insert(index, value);
768800
}
769801

@@ -774,6 +806,10 @@ public void Insert(int index, BsonValue value)
774806
/// <returns>True if the value was removed.</returns>
775807
public bool Remove(BsonValue value)
776808
{
809+
if (value == null)
810+
{
811+
throw new ArgumentNullException("value");
812+
}
777813
return _values.Remove(value);
778814
}
779815

Bson/ObjectModel/BsonBinaryData.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public BsonBinaryData(byte[] bytes, BsonBinarySubType subType)
6060
public BsonBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation)
6161
: base(BsonType.Binary)
6262
{
63+
if (bytes == null)
64+
{
65+
throw new ArgumentNullException("bytes");
66+
}
6367
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
6468
{
6569
if (bytes.Length != 16)
@@ -165,11 +169,11 @@ public BsonBinarySubType SubType
165169
/// <summary>
166170
/// Converts a byte array to a BsonBinaryData.
167171
/// </summary>
168-
/// <param name="value">A byte array.</param>
172+
/// <param name="bytes">A byte array.</param>
169173
/// <returns>A BsonBinaryData.</returns>
170-
public static implicit operator BsonBinaryData(byte[] value)
174+
public static implicit operator BsonBinaryData(byte[] bytes)
171175
{
172-
return BsonBinaryData.Create(value);
176+
return BsonBinaryData.Create(bytes);
173177
}
174178

175179
/// <summary>

0 commit comments

Comments
 (0)