Skip to content

Commit

Permalink
Initial work on CSHARP-415 and CSHARP-417. BsonTypeMapper is now bidi…
Browse files Browse the repository at this point in the history
…rectional 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.
  • Loading branch information
Robert Stam committed Mar 29, 2012
1 parent 08224bc commit 89db3dd
Show file tree
Hide file tree
Showing 24 changed files with 761 additions and 192 deletions.
1 change: 1 addition & 0 deletions Bson/Bson.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="ObjectModel\GuidConverter.cs" />
<Compile Include="IO\JsonReaderSettings.cs" />
<Compile Include="ObjectModel\ICustomBsonTypeMapper.cs" />
<Compile Include="ObjectModel\BsonTypeMapperOptions.cs" />
<Compile Include="Serialization\Attributes\BsonDateTimeOptionsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonDictionaryOptionsAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonExtraElementsAttribute.cs" />
Expand Down
44 changes: 40 additions & 4 deletions Bson/ObjectModel/BsonArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,13 @@ public IEnumerable<BsonValue> Values
public BsonValue this[int index]
{
get { return _values[index]; }
set { _values[index] = value; }
set {
if (value == null)
{
throw new ArgumentNullException("value");
}
_values[index] = value;
}
}

// public static methods
Expand Down Expand Up @@ -450,7 +456,13 @@ public BsonArray AddRange(IEnumerable<BsonValue> values)
{
if (values != null)
{
_values.AddRange(values);
foreach (var value in values)
{
if (value != null)
{
_values.Add(value);
}
}
}
return this;
}
Expand Down Expand Up @@ -551,7 +563,7 @@ public BsonArray AddRange(IEnumerable<string> values)
{
foreach (var value in values)
{
_values.Add(BsonString.Create(value));
_values.Add((value == null) ? (BsonValue)BsonNull.Value : BsonString.Create(value));
}
}
return this;
Expand All @@ -568,7 +580,7 @@ public BsonArray AddRange(IEnumerable values)
{
foreach (var value in values)
{
_values.Add(BsonValue.Create(value));
_values.Add(BsonTypeMapper.MapToBsonValue(value));
}
}
return this;
Expand Down Expand Up @@ -635,6 +647,10 @@ public override int CompareTo(BsonValue other)
/// <returns>True if the array contains the value.</returns>
public bool Contains(BsonValue value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return _values.Contains(value);
}

Expand Down Expand Up @@ -731,6 +747,10 @@ public override int GetHashCode()
/// <returns>The zero based index of the value (or -1 if not found).</returns>
public int IndexOf(BsonValue value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return _values.IndexOf(value);
}

Expand All @@ -742,6 +762,10 @@ public int IndexOf(BsonValue value)
/// <returns>The zero based index of the value (or -1 if not found).</returns>
public int IndexOf(BsonValue value, int index)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return _values.IndexOf(value, index);
}

Expand All @@ -754,6 +778,10 @@ public int IndexOf(BsonValue value, int index)
/// <returns>The zero based index of the value (or -1 if not found).</returns>
public int IndexOf(BsonValue value, int index, int count)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return _values.IndexOf(value, index, count);
}

Expand All @@ -764,6 +792,10 @@ public int IndexOf(BsonValue value, int index, int count)
/// <param name="value">The new value.</param>
public void Insert(int index, BsonValue value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_values.Insert(index, value);
}

Expand All @@ -774,6 +806,10 @@ public void Insert(int index, BsonValue value)
/// <returns>True if the value was removed.</returns>
public bool Remove(BsonValue value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return _values.Remove(value);
}

Expand Down
10 changes: 7 additions & 3 deletions Bson/ObjectModel/BsonBinaryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public BsonBinaryData(byte[] bytes, BsonBinarySubType subType)
public BsonBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation)
: base(BsonType.Binary)
{
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
{
if (bytes.Length != 16)
Expand Down Expand Up @@ -165,11 +169,11 @@ public BsonBinarySubType SubType
/// <summary>
/// Converts a byte array to a BsonBinaryData.
/// </summary>
/// <param name="value">A byte array.</param>
/// <param name="bytes">A byte array.</param>
/// <returns>A BsonBinaryData.</returns>
public static implicit operator BsonBinaryData(byte[] value)
public static implicit operator BsonBinaryData(byte[] bytes)
{
return BsonBinaryData.Create(value);
return BsonBinaryData.Create(bytes);
}

/// <summary>
Expand Down
Loading

2 comments on commit 89db3dd

@almostEric
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting this error now:
"An error occurred while deserializing the MenuItemProductDetail property of class BeerDashboard.Common.Models.MenuItem: A serialization options attribute of type BsonDictionaryOptionsAttribute cannot be used when the serializer is of type EnumerableSerializer and the item serializer is of type BsonClassMapSerializer."
Here are the two classes in question:

public class MenuItemProductDetail
{
    public Guid Id { get; set; }

    public Beer Beer { get; set; }

    public string ProductId { get; set; }

    [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
    public List<ItemPrice> Prices { get; set; }

    public string Notes { get; set; }

    public string EventId { get; set; }

    public bool DoNotUse { get; set; }
}


public class ItemPrice
{
    public int Size { get; set; }

    public double Price { get; set; }

    public string DisplayName { get; set; }

    public string Plu { get; set; }

    public string Glassware { get; set; }
}

@rstam
Copy link
Contributor

@rstam rstam commented on 89db3dd Mar 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a result of better error checking in the latest commits. Just remove the BsonDictionaryOptions attribute from your Prices property as it does not apply to either the array or the items.

Please sign in to comment.