1
1
package jsoniter
2
2
3
3
import (
4
+ "fmt"
4
5
"math"
5
6
"strconv"
6
7
)
@@ -13,6 +14,10 @@ func init() {
13
14
14
15
// WriteFloat32 write float32 to stream
15
16
func (stream * Stream ) WriteFloat32 (val float32 ) {
17
+ if math .IsInf (float64 (val ), 0 ) || math .IsNaN (float64 (val )) {
18
+ stream .Error = fmt .Errorf ("unsupported value: %f" , val )
19
+ return
20
+ }
16
21
abs := math .Abs (float64 (val ))
17
22
fmt := byte ('f' )
18
23
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
@@ -26,6 +31,10 @@ func (stream *Stream) WriteFloat32(val float32) {
26
31
27
32
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
28
33
func (stream * Stream ) WriteFloat32Lossy (val float32 ) {
34
+ if math .IsInf (float64 (val ), 0 ) || math .IsNaN (float64 (val )) {
35
+ stream .Error = fmt .Errorf ("unsupported value: %f" , val )
36
+ return
37
+ }
29
38
if val < 0 {
30
39
stream .writeByte ('-' )
31
40
val = - val
@@ -54,6 +63,10 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
54
63
55
64
// WriteFloat64 write float64 to stream
56
65
func (stream * Stream ) WriteFloat64 (val float64 ) {
66
+ if math .IsInf (val , 0 ) || math .IsNaN (val ) {
67
+ stream .Error = fmt .Errorf ("unsupported value: %f" , val )
68
+ return
69
+ }
57
70
abs := math .Abs (val )
58
71
fmt := byte ('f' )
59
72
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
@@ -67,6 +80,10 @@ func (stream *Stream) WriteFloat64(val float64) {
67
80
68
81
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
69
82
func (stream * Stream ) WriteFloat64Lossy (val float64 ) {
83
+ if math .IsInf (val , 0 ) || math .IsNaN (val ) {
84
+ stream .Error = fmt .Errorf ("unsupported value: %f" , val )
85
+ return
86
+ }
70
87
if val < 0 {
71
88
stream .writeByte ('-' )
72
89
val = - val
0 commit comments