Skip to content

Commit f3e230e

Browse files
author
rstam
committed
Minor refactoring and standardization of error messages.
1 parent e99951f commit f3e230e

File tree

7 files changed

+66
-51
lines changed

7 files changed

+66
-51
lines changed

Bson/ObjectModel/BsonDocument.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,11 +741,16 @@ public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializ
741741
}
742742
else
743743
{
744-
var documentSerializationOptions = options as DocumentSerializationOptions;
745-
if (documentSerializationOptions != null)
744+
var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
745+
if (documentSerializationOptions == null)
746746
{
747-
_allowDuplicateNames = documentSerializationOptions.AllowDuplicateNames;
747+
var message = string.Format(
748+
"Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
749+
BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
750+
BsonUtils.GetFriendlyTypeName(options.GetType()));
751+
throw new BsonSerializationException(message);
748752
}
753+
_allowDuplicateNames = documentSerializationOptions.AllowDuplicateNames;
749754

750755
bsonReader.ReadStartDocument();
751756
Clear();

Bson/Serialization/Serializers/BsonPrimitiveSerializers.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public override void Serialize(
127127
bsonWriter.WriteString(XmlConvert.ToString(boolValue));
128128
break;
129129
default:
130-
var message = string.Format("'{0}' is not a valid representation for type Boolean.", representationSerializationOptions.Representation);
130+
var message = string.Format("'{0}' is not a valid Boolean representation.", representationSerializationOptions.Representation);
131131
throw new BsonSerializationException(message);
132132
}
133133
}
@@ -303,7 +303,7 @@ public override void Serialize(
303303
}
304304
break;
305305
default:
306-
var message = string.Format("'{0}' is not a valid representation for type DateTime.", dateTimeSerializationOptions.Representation);
306+
var message = string.Format("'{0}' is not a valid DateTime representation.", dateTimeSerializationOptions.Representation);
307307
throw new BsonSerializationException(message);
308308
}
309309
}
@@ -401,7 +401,7 @@ public override void Serialize(
401401
bsonWriter.WriteString(doubleValue.ToString("R", NumberFormatInfo.InvariantInfo));
402402
break;
403403
default:
404-
var message = string.Format("'{0}' is not a valid representation for type Double.", representationSerializationOptions.Representation);
404+
var message = string.Format("'{0}' is not a valid Double representation.", representationSerializationOptions.Representation);
405405
throw new BsonSerializationException(message);
406406
}
407407
}
@@ -791,9 +791,9 @@ public override void Serialize(
791791
IBsonSerializationOptions options)
792792
{
793793
var objectId = (ObjectId)value;
794-
var representation = (options == null) ? BsonType.ObjectId : ((RepresentationSerializationOptions)options).Representation;
794+
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
795795

796-
switch (representation)
796+
switch (representationSerializationOptions.Representation)
797797
{
798798
case BsonType.ObjectId:
799799
bsonWriter.WriteObjectId(objectId.Timestamp, objectId.Machine, objectId.Pid, objectId.Increment);
@@ -802,7 +802,8 @@ public override void Serialize(
802802
bsonWriter.WriteString(objectId.ToString());
803803
break;
804804
default:
805-
throw new BsonInternalException("Unexpected representation.");
805+
var message = string.Format("'{0}' is not a valid ObjectId representation.", representationSerializationOptions.Representation);
806+
throw new BsonSerializationException(message);
806807
}
807808
}
808809
}
@@ -849,6 +850,7 @@ public override object Deserialize(
849850
IBsonSerializationOptions options)
850851
{
851852
VerifyTypes(nominalType, actualType, typeof(string));
853+
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
852854

853855
var bsonType = bsonReader.GetCurrentBsonType();
854856
if (bsonType == BsonType.Null)
@@ -858,15 +860,21 @@ public override object Deserialize(
858860
}
859861
else
860862
{
861-
var representation = (options == null) ? BsonType.String : ((RepresentationSerializationOptions)options).Representation;
862-
switch (representation)
863+
switch (bsonType)
863864
{
864865
case BsonType.ObjectId:
865-
int timestamp, machine, increment;
866-
short pid;
867-
bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment);
868-
var objectId = new ObjectId(timestamp, machine, pid, increment);
869-
return objectId.ToString();
866+
if (representationSerializationOptions.Representation == BsonType.ObjectId)
867+
{
868+
int timestamp, machine, increment;
869+
short pid;
870+
bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment);
871+
var objectId = new ObjectId(timestamp, machine, pid, increment);
872+
return objectId.ToString();
873+
}
874+
else
875+
{
876+
goto default;
877+
}
870878
case BsonType.String:
871879
return bsonReader.ReadString();
872880
case BsonType.Symbol:
@@ -898,9 +906,9 @@ public override void Serialize(
898906
else
899907
{
900908
var stringValue = (string)value;
901-
var representation = (options == null) ? BsonType.String : ((RepresentationSerializationOptions)options).Representation;
909+
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
902910

903-
switch (representation)
911+
switch (representationSerializationOptions.Representation)
904912
{
905913
case BsonType.ObjectId:
906914
var id = ObjectId.Parse(stringValue);
@@ -913,7 +921,8 @@ public override void Serialize(
913921
bsonWriter.WriteSymbol(stringValue);
914922
break;
915923
default:
916-
throw new BsonInternalException("Unexpected representation.");
924+
var message = string.Format("'{0}' is not a valid String representation.", representationSerializationOptions.Representation);
925+
throw new BsonSerializationException(message);
917926
}
918927
}
919928
}

Bson/Serialization/Serializers/BsonValueSerializers.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public override void Serialize(
191191
var bytes = binaryData.Bytes;
192192
var subType = binaryData.SubType;
193193
var guidRepresentation = binaryData.GuidRepresentation;
194+
194195
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
195196
{
196197
var writerGuidRepresentation = bsonWriter.Settings.GuidRepresentation;
@@ -212,6 +213,7 @@ public override void Serialize(
212213
}
213214
}
214215
}
216+
215217
bsonWriter.WriteBinaryData(bytes, subType, guidRepresentation);
216218
}
217219
}
@@ -339,6 +341,7 @@ public override object Deserialize(
339341
IBsonSerializationOptions options)
340342
{
341343
VerifyTypes(nominalType, actualType, typeof(BsonDateTime));
344+
var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);
342345

343346
var bsonType = bsonReader.GetCurrentBsonType();
344347
if (bsonType == BsonType.Null)
@@ -348,8 +351,6 @@ public override object Deserialize(
348351
}
349352
else
350353
{
351-
var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);
352-
353354
long? millisecondsSinceEpoch = null;
354355
long? ticks = null;
355356
switch (bsonType)
@@ -532,7 +533,7 @@ public override void Serialize(
532533
}
533534
break;
534535
default:
535-
var message = string.Format("'{0}' is not a valid representation for type DateTime.", dateTimeSerializationOptions.Representation);
536+
var message = string.Format("'{0}' is not a valid DateTime representation.", dateTimeSerializationOptions.Representation);
536537
throw new BsonSerializationException(message);
537538
}
538539
}

Bson/Serialization/Serializers/DictionaryGenericSerializer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ public override void Serialize(
290290
break;
291291
default:
292292
var message = string.Format(
293-
"'{0}' is not a valid representation for type IDictionary<{1}, {2}>.",
294-
representation, typeof(TKey).Name, typeof(TValue).Name);
293+
"'{0}' is not a valid {1} representation.",
294+
representation,
295+
BsonUtils.GetFriendlyTypeName(typeof(IDictionary<TKey, TValue>)));
295296
throw new BsonSerializationException(message);
296297
}
297298
}

Bson/Serialization/Serializers/DictionarySerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public override void Serialize(
292292
bsonWriter.WriteEndArray();
293293
break;
294294
default:
295-
var message = string.Format("'{0}' is not a valid representation for type IDictionary.", representation);
295+
var message = string.Format("'{0}' is not a valid IDictionary representation.", representation);
296296
throw new BsonSerializationException(message);
297297
}
298298
}

Bson/Serialization/Serializers/EnumSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
6565
IBsonSerializationOptions options)
6666
{
6767
VerifyDeserializeType(nominalType);
68+
6869
var bsonType = bsonReader.GetCurrentBsonType();
6970
switch (bsonType)
7071
{

0 commit comments

Comments
 (0)