File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -75,5 +75,24 @@ public void Json_Number_Deserialize_Test()
7575 JsonSerializer . Deserialize ( positiveDouble . ToString ( "F1" , CultureInfo . InvariantCulture ) ) . Should ( ) . Be ( positiveDouble ) ;
7676 JsonSerializer . Deserialize ( negativeDouble . ToString ( "F1" , CultureInfo . InvariantCulture ) ) . Should ( ) . Be ( negativeDouble ) ;
7777 }
78+
79+ [ Fact ]
80+ public void Json_DoubleNaN_Tests ( )
81+ {
82+ BsonDocument doc = new BsonDocument ( ) ;
83+ doc [ "doubleNaN" ] = double . NaN ;
84+ doc [ "doubleNegativeInfinity" ] = double . NegativeInfinity ;
85+ doc [ "doublePositiveInfinity" ] = double . PositiveInfinity ;
86+
87+ // Convert to JSON
88+ string json = JsonSerializer . Serialize ( doc ) ;
89+
90+ var bson = JsonSerializer . Deserialize ( json ) ;
91+
92+ // JSON standard converts NaN and Infinities to null, so deserialized values should not be double.NaN nor double.*Infinity
93+ Assert . False ( double . IsNaN ( bson [ "doubleNaN" ] . AsDouble ) ) ;
94+ Assert . False ( double . IsNegativeInfinity ( bson [ "doubleNegativeInfinity" ] . AsDouble ) ) ;
95+ Assert . False ( double . IsPositiveInfinity ( bson [ "doublePositiveInfinity" ] . AsDouble ) ) ;
96+ }
7897 }
7998}
Original file line number Diff line number Diff line change @@ -71,7 +71,17 @@ private void WriteValue(BsonValue value)
7171 break ;
7272
7373 case BsonType . Double :
74- _writer . Write ( value . AsDouble . ToString ( "0.0########" , _numberFormat ) ) ;
74+ var d = value . AsDouble ;
75+
76+ if ( double . IsNaN ( d ) || double . IsNegativeInfinity ( d ) || double . IsPositiveInfinity ( d ) )
77+ {
78+ _writer . Write ( "null" ) ;
79+ }
80+ else
81+ {
82+ _writer . Write ( value . AsDouble . ToString ( "0.0########" , _numberFormat ) ) ;
83+ }
84+
7585 break ;
7686
7787 case BsonType . Binary :
You can’t perform that action at this time.
0 commit comments