File tree 2 files changed +22
-1
lines changed
2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 17
17
import base64
18
18
import datetime
19
19
import decimal
20
+ import math
20
21
import re
21
22
22
23
from google .cloud ._helpers import UTC
@@ -305,7 +306,12 @@ def _int_to_json(value):
305
306
306
307
def _float_to_json (value ):
307
308
"""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 )
309
315
310
316
311
317
def _decimal_to_json (value ):
Original file line number Diff line number Diff line change @@ -656,9 +656,24 @@ def _call_fut(self, value):
656
656
657
657
return _float_to_json (value )
658
658
659
+ def test_w_none (self ):
660
+ self .assertEqual (self ._call_fut (None ), None )
661
+
659
662
def test_w_float (self ):
660
663
self .assertEqual (self ._call_fut (1.23 ), 1.23 )
661
664
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
+
662
677
663
678
class Test_decimal_to_json (unittest .TestCase ):
664
679
def _call_fut (self , value ):
You can’t perform that action at this time.
0 commit comments