@@ -127,7 +127,7 @@ public override void Serialize(
127
127
bsonWriter . WriteString ( XmlConvert . ToString ( boolValue ) ) ;
128
128
break ;
129
129
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 ) ;
131
131
throw new BsonSerializationException ( message ) ;
132
132
}
133
133
}
@@ -303,7 +303,7 @@ public override void Serialize(
303
303
}
304
304
break ;
305
305
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 ) ;
307
307
throw new BsonSerializationException ( message ) ;
308
308
}
309
309
}
@@ -401,7 +401,7 @@ public override void Serialize(
401
401
bsonWriter . WriteString ( doubleValue . ToString ( "R" , NumberFormatInfo . InvariantInfo ) ) ;
402
402
break ;
403
403
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 ) ;
405
405
throw new BsonSerializationException ( message ) ;
406
406
}
407
407
}
@@ -791,9 +791,9 @@ public override void Serialize(
791
791
IBsonSerializationOptions options )
792
792
{
793
793
var objectId = ( ObjectId ) value ;
794
- var representation = ( options == null ) ? BsonType . ObjectId : ( ( RepresentationSerializationOptions ) options ) . Representation ;
794
+ var representationSerializationOptions = EnsureSerializationOptions < RepresentationSerializationOptions > ( options ) ;
795
795
796
- switch ( representation )
796
+ switch ( representationSerializationOptions . Representation )
797
797
{
798
798
case BsonType . ObjectId :
799
799
bsonWriter . WriteObjectId ( objectId . Timestamp , objectId . Machine , objectId . Pid , objectId . Increment ) ;
@@ -802,7 +802,8 @@ public override void Serialize(
802
802
bsonWriter . WriteString ( objectId . ToString ( ) ) ;
803
803
break ;
804
804
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 ) ;
806
807
}
807
808
}
808
809
}
@@ -849,6 +850,7 @@ public override object Deserialize(
849
850
IBsonSerializationOptions options )
850
851
{
851
852
VerifyTypes ( nominalType , actualType , typeof ( string ) ) ;
853
+ var representationSerializationOptions = EnsureSerializationOptions < RepresentationSerializationOptions > ( options ) ;
852
854
853
855
var bsonType = bsonReader . GetCurrentBsonType ( ) ;
854
856
if ( bsonType == BsonType . Null )
@@ -858,15 +860,21 @@ public override object Deserialize(
858
860
}
859
861
else
860
862
{
861
- var representation = ( options == null ) ? BsonType . String : ( ( RepresentationSerializationOptions ) options ) . Representation ;
862
- switch ( representation )
863
+ switch ( bsonType )
863
864
{
864
865
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
+ }
870
878
case BsonType . String :
871
879
return bsonReader . ReadString ( ) ;
872
880
case BsonType . Symbol :
@@ -898,9 +906,9 @@ public override void Serialize(
898
906
else
899
907
{
900
908
var stringValue = ( string ) value ;
901
- var representation = ( options == null ) ? BsonType . String : ( ( RepresentationSerializationOptions ) options ) . Representation ;
909
+ var representationSerializationOptions = EnsureSerializationOptions < RepresentationSerializationOptions > ( options ) ;
902
910
903
- switch ( representation )
911
+ switch ( representationSerializationOptions . Representation )
904
912
{
905
913
case BsonType . ObjectId :
906
914
var id = ObjectId . Parse ( stringValue ) ;
@@ -913,7 +921,8 @@ public override void Serialize(
913
921
bsonWriter . WriteSymbol ( stringValue ) ;
914
922
break ;
915
923
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 ) ;
917
926
}
918
927
}
919
928
}
0 commit comments