File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 1717import base64
1818import datetime
1919import decimal
20+ import math
2021import re
2122
2223from google .cloud ._helpers import UTC
@@ -305,7 +306,12 @@ def _int_to_json(value):
305306
306307def _float_to_json (value ):
307308 """Coerce 'value' to an JSON-compatible representation."""
308- return value if value is None else float (value )
309+ if value is None :
310+ return None
311+ elif math .isnan (value ) or math .isinf (value ):
312+ return str (value )
313+ else :
314+ return float (value )
309315
310316
311317def _decimal_to_json (value ):
Original file line number Diff line number Diff line change @@ -656,9 +656,24 @@ def _call_fut(self, value):
656656
657657 return _float_to_json (value )
658658
659+ def test_w_none (self ):
660+ self .assertEqual (self ._call_fut (None ), None )
661+
659662 def test_w_float (self ):
660663 self .assertEqual (self ._call_fut (1.23 ), 1.23 )
661664
665+ def test_w_nan (self ):
666+ result = self ._call_fut (float ("nan" ))
667+ self .assertEqual (result .lower (), "nan" )
668+
669+ def test_w_infinity (self ):
670+ result = self ._call_fut (float ("inf" ))
671+ self .assertEqual (result .lower (), "inf" )
672+
673+ def test_w_negative_infinity (self ):
674+ result = self ._call_fut (float ("-inf" ))
675+ self .assertEqual (result .lower (), "-inf" )
676+
662677
663678class Test_decimal_to_json (unittest .TestCase ):
664679 def _call_fut (self , value ):
You can’t perform that action at this time.
0 commit comments