encode: ensure floats have decimal point (#615)

Fixes #571

Co-authored-by: Sterling Hanenkamp <sterling@ziprecruiter.com>
This commit is contained in:
Sterling Hanenkamp
2021-10-14 07:34:54 -05:00
committed by GitHub
parent 86632bc190
commit 4984dcb5e9
2 changed files with 34 additions and 2 deletions
+11 -2
View File
@@ -5,6 +5,7 @@ import (
"encoding"
"fmt"
"io"
"math"
"reflect"
"sort"
"strconv"
@@ -244,9 +245,17 @@ func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, e
case reflect.String:
b = enc.encodeString(b, v.String(), ctx.options)
case reflect.Float32:
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 32)
if math.Trunc(v.Float()) == v.Float() {
b = strconv.AppendFloat(b, v.Float(), 'f', 1, 32)
} else {
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 32)
}
case reflect.Float64:
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 64)
if math.Trunc(v.Float()) == v.Float() {
b = strconv.AppendFloat(b, v.Float(), 'f', 1, 64)
} else {
b = strconv.AppendFloat(b, v.Float(), 'f', -1, 64)
}
case reflect.Bool:
if v.Bool() {
b = append(b, "true"...)