Permalink
Browse files

More work on CSHARP-460. Added KeyValueSerializationOptions to Dictio…

…narySerializationOptions.
  • Loading branch information...
1 parent 88c5eed commit 56c9ee8005def3f04c0b060a868e0f560969aaff rstam committed with May 24, 2012
@@ -58,14 +58,15 @@ public class DictionarySerializationOptions : BsonBaseSerializationOptions
// private fields
private DictionaryRepresentation _representation = DictionaryRepresentation.Dynamic;
- private IBsonSerializationOptions _valueSerializationOptions;
+ private KeyValuePairSerializationOptions _keyValuePairSerializationOptions;
// constructors
/// <summary>
/// Initializes a new instance of the DictionarySerializationOptions class.
/// </summary>
public DictionarySerializationOptions()
{
+ _keyValuePairSerializationOptions = (KeyValuePairSerializationOptions)KeyValuePairSerializationOptions.Defaults.Clone();
}
/// <summary>
@@ -75,17 +76,22 @@ public DictionarySerializationOptions()
public DictionarySerializationOptions(DictionaryRepresentation representation)
{
_representation = representation;
+ _keyValuePairSerializationOptions = (KeyValuePairSerializationOptions)KeyValuePairSerializationOptions.Defaults.Clone();
}
/// <summary>
/// Initializes a new instance of the DictionarySerializationOptions class.
/// </summary>
/// <param name="representation">The representation to use for a Dictionary.</param>
- /// <param name="valueSerializationOptions">The serialization options for the values in the dictionary.</param>
- public DictionarySerializationOptions(DictionaryRepresentation representation, IBsonSerializationOptions valueSerializationOptions)
+ /// <param name="keyValuePairSerializationOptions">The serialization options for the key/value pairs in the dictionary.</param>
+ public DictionarySerializationOptions(DictionaryRepresentation representation, KeyValuePairSerializationOptions keyValuePairSerializationOptions)
{
+ if (keyValuePairSerializationOptions == null)
+ {
+ throw new ArgumentNullException("keyValuePairSerializationOptions");
+ }
_representation = representation;
- _valueSerializationOptions = valueSerializationOptions;
+ _keyValuePairSerializationOptions = keyValuePairSerializationOptions;
}
// public static properties
@@ -111,7 +117,13 @@ public static DictionarySerializationOptions ArrayOfDocuments
public static DictionarySerializationOptions Defaults
{
get { return __defaults; }
- set { __defaults = value; }
+ set {
+ if (value == null)
+ {
+ throw new ArgumentNullException("value");
+ }
+ __defaults = value;
+ }
}
/// <summary>
@@ -134,11 +146,20 @@ public static DictionarySerializationOptions Dynamic
/// <summary>
/// Gets or sets the serialization options for the values in the dictionary.
/// </summary>
- [Obsolete("Use ValueSerializationOptions instead.")]
+ [Obsolete("Use KeyValuePairSerializationOptions instead.")]
public IBsonSerializationOptions ItemSerializationOptions
{
- get { return ValueSerializationOptions; }
- set { ValueSerializationOptions = value; }
+ get { return _keyValuePairSerializationOptions.ValueSerializationOptions; }
+ set {
+ if (value == null)
+ {
+ throw new ArgumentNullException("value");
+ }
+ _keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
+ _keyValuePairSerializationOptions.Representation,
+ _keyValuePairSerializationOptions.KeySerializationOptions,
+ value);
+ }
}
/// <summary>
@@ -157,13 +178,17 @@ public DictionaryRepresentation Representation
/// <summary>
/// Gets or sets the serialization options for the values in the dictionary.
/// </summary>
- public IBsonSerializationOptions ValueSerializationOptions
+ public KeyValuePairSerializationOptions KeyValuePairSerializationOptions
{
- get { return _valueSerializationOptions; }
+ get { return _keyValuePairSerializationOptions; }
set
{
+ if (value == null)
+ {
+ throw new ArgumentNullException("value");
+ }
EnsureNotFrozen();
- _valueSerializationOptions = value;
+ _keyValuePairSerializationOptions = value;
}
}
@@ -176,6 +201,7 @@ public IBsonSerializationOptions ValueSerializationOptions
public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute)
{
EnsureNotFrozen();
+
var dictionaryOptionsAttribute = attribute as BsonDictionaryOptionsAttribute;
if (dictionaryOptionsAttribute != null)
{
@@ -198,14 +224,16 @@ public override void ApplyAttribute(IBsonSerializer serializer, Attribute attrib
}
}
+ // any other attributes are applied to the values
var valueType = typeof(object);
if (serializer.GetType().IsGenericType)
{
- valueType = serializer.GetType().GetGenericArguments()[1];
+ valueType = serializer.GetType().GetGenericArguments()[1]; // TValue
}
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
- if (_valueSerializationOptions == null)
+ var valueSerializationOptions = _keyValuePairSerializationOptions.ValueSerializationOptions;
+ if (valueSerializationOptions == null)
{
var valueDefaultSerializationOptions = valueSerializer.GetDefaultSerializationOptions();
@@ -227,10 +255,14 @@ public override void ApplyAttribute(IBsonSerializer serializer, Attribute attrib
throw new NotSupportedException(message);
}
- _valueSerializationOptions = valueDefaultSerializationOptions.Clone();
+ valueSerializationOptions = valueDefaultSerializationOptions.Clone();
}
- _valueSerializationOptions.ApplyAttribute(valueSerializer, attribute);
+ valueSerializationOptions.ApplyAttribute(valueSerializer, attribute);
+ _keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
+ _keyValuePairSerializationOptions.Representation,
+ _keyValuePairSerializationOptions.KeySerializationOptions,
+ valueSerializationOptions);
}
/// <summary>
@@ -239,7 +271,7 @@ public override void ApplyAttribute(IBsonSerializer serializer, Attribute attrib
/// <returns>A cloned copy of the serialization options.</returns>
public override IBsonSerializationOptions Clone()
{
- return new DictionarySerializationOptions(_representation, _valueSerializationOptions);
+ return new DictionarySerializationOptions(_representation, _keyValuePairSerializationOptions);
}
/// <summary>
@@ -250,9 +282,9 @@ public override IBsonSerializationOptions Freeze()
{
if (!IsFrozen)
{
- if (_valueSerializationOptions != null)
+ if (_keyValuePairSerializationOptions != null)
{
- _valueSerializationOptions.Freeze();
+ _keyValuePairSerializationOptions.Freeze();
}
}
return base.Freeze();
@@ -62,8 +62,7 @@ public DictionarySerializer()
{
var dictionarySerializationOptions = EnsureSerializationOptions(options);
var dictionaryRepresentation = dictionarySerializationOptions.Representation;
- var keySerializationOptions = (IBsonSerializationOptions)null; // TODO: how should these be provided?
- var valueSerializationOptions = dictionarySerializationOptions.ValueSerializationOptions;
+ var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
@@ -92,7 +91,7 @@ public DictionarySerializer()
var key = (TKey)(object)bsonReader.ReadName();
var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue));
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
- var value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, valueSerializationOptions);
+ var value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, keyValuePairSerializationOptions.ValueSerializationOptions);
dictionary.Add(key, value);
}
bsonReader.ReadEndDocument();
@@ -102,10 +101,6 @@ public DictionarySerializer()
else if (bsonType == BsonType.Array)
{
var dictionary = CreateInstance(actualType);
- var keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
- (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document,
- keySerializationOptions,
- valueSerializationOptions);
bsonReader.ReadStartArray();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
@@ -160,8 +155,7 @@ public DictionarySerializer()
var dictionary = (IDictionary<TKey, TValue>)value;
var dictionarySerializationOptions = EnsureSerializationOptions(options);
var dictionaryRepresentation = dictionarySerializationOptions.Representation;
- var keySerializationOptions = (IBsonSerializationOptions)null; // TODO: how should these be provided?
- var valueSerializationOptions = dictionarySerializationOptions.ValueSerializationOptions;
+ var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;
if (dictionaryRepresentation == DictionaryRepresentation.Dynamic)
{
@@ -191,17 +185,22 @@ public DictionarySerializer()
foreach (var keyValuePair in dictionary)
{
bsonWriter.WriteName((string)(object)keyValuePair.Key);
- BsonSerializer.Serialize(bsonWriter, typeof(TValue), keyValuePair.Value, valueSerializationOptions);
+ BsonSerializer.Serialize(bsonWriter, typeof(TValue), keyValuePair.Value, keyValuePairSerializationOptions.ValueSerializationOptions);
}
bsonWriter.WriteEndDocument();
break;
case DictionaryRepresentation.ArrayOfArrays:
case DictionaryRepresentation.ArrayOfDocuments:
- var keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
- (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document,
- keySerializationOptions,
- valueSerializationOptions);
+ // override KeyValuePair representation if necessary
+ var keyValuePairRepresentation = (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document;
+ if (keyValuePairSerializationOptions.Representation != keyValuePairRepresentation)
+ {
+ keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
+ keyValuePairRepresentation,
+ keyValuePairSerializationOptions.KeySerializationOptions,
+ keyValuePairSerializationOptions.ValueSerializationOptions);
+ }
bsonWriter.WriteStartArray();
foreach (var keyValuePair in dictionary)
@@ -74,8 +74,7 @@ public static DictionarySerializer Instance
{
var dictionarySerializationOptions = EnsureSerializationOptions(options);
var dictionaryRepresentation = dictionarySerializationOptions.Representation;
- var keySerializationOptions = (IBsonSerializationOptions)null; // TODO: how should these be provided?
- var valueSerializationOptions = dictionarySerializationOptions.ValueSerializationOptions;
+ var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
@@ -104,7 +103,7 @@ public static DictionarySerializer Instance
var key = bsonReader.ReadName();
var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(object));
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
- var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, valueSerializationOptions);
+ var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, keyValuePairSerializationOptions.ValueSerializationOptions);
dictionary.Add(key, value);
}
bsonReader.ReadEndDocument();
@@ -114,10 +113,6 @@ public static DictionarySerializer Instance
else if (bsonType == BsonType.Array)
{
var dictionary = CreateInstance(actualType);
- var keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
- (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document,
- keySerializationOptions,
- valueSerializationOptions);
bsonReader.ReadStartArray();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
@@ -172,8 +167,7 @@ public static DictionarySerializer Instance
var dictionary = (IDictionary)value;
var dictionarySerializationOptions = EnsureSerializationOptions(options);
var dictionaryRepresentation = dictionarySerializationOptions.Representation;
- var keySerializationOptions = (IBsonSerializationOptions)null; // TODO: how should these be provided?
- var valueSerializationOptions = dictionarySerializationOptions.ValueSerializationOptions;
+ var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;
if (dictionaryRepresentation == DictionaryRepresentation.Dynamic)
{
@@ -196,16 +190,23 @@ public static DictionarySerializer Instance
foreach (DictionaryEntry dictionaryEntry in dictionary)
{
bsonWriter.WriteName((string)dictionaryEntry.Key);
- BsonSerializer.Serialize(bsonWriter, typeof(object), dictionaryEntry.Value, valueSerializationOptions);
+ BsonSerializer.Serialize(bsonWriter, typeof(object), dictionaryEntry.Value, keyValuePairSerializationOptions.ValueSerializationOptions);
}
bsonWriter.WriteEndDocument();
break;
+
case DictionaryRepresentation.ArrayOfArrays:
case DictionaryRepresentation.ArrayOfDocuments:
- var keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
- (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document,
- keySerializationOptions,
- valueSerializationOptions);
+ // override KeyValuePair representation if necessary
+ var keyValuePairRepresentation = (dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays) ? BsonType.Array : BsonType.Document;
+ if (keyValuePairSerializationOptions.Representation != keyValuePairRepresentation)
+ {
+ keyValuePairSerializationOptions = new KeyValuePairSerializationOptions(
+ keyValuePairRepresentation,
+ keyValuePairSerializationOptions.KeySerializationOptions,
+ keyValuePairSerializationOptions.ValueSerializationOptions);
+ }
+
bsonWriter.WriteStartArray();
foreach (DictionaryEntry dictionaryEntry in dictionary)
{

0 comments on commit 56c9ee8

Please sign in to comment.