Skip to content

Commit 7f4797e

Browse files
author
Isabella Siu
committed
GODRIVER-756 fix formatting of numbers with exponents in JSON
Change-Id: I9c8fe82ab5ddc735206228abfd7bc943e143f41a
1 parent 75d4a13 commit 7f4797e

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

bson/bson.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
package bson
1313

1414
import (
15-
"math"
16-
"strconv"
17-
"strings"
18-
1915
"github.com/mongodb/mongo-go-driver/bson/primitive"
2016
)
2117

@@ -62,23 +58,3 @@ type M = primitive.M
6258
// bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
6359
//
6460
type A = primitive.A
65-
66-
func formatDouble(f float64) string {
67-
var s string
68-
if math.IsInf(f, 1) {
69-
s = "Infinity"
70-
} else if math.IsInf(f, -1) {
71-
s = "-Infinity"
72-
} else if math.IsNaN(f) {
73-
s = "NaN"
74-
} else {
75-
// Print exactly one decimalType place for integers; otherwise, print as many are necessary to
76-
// perfectly represent it.
77-
s = strconv.FormatFloat(f, 'G', -1, 64)
78-
if !strings.ContainsRune(s, '.') {
79-
s += ".0"
80-
}
81-
}
82-
83-
return s
84-
}

bson/bson_corpus_spec_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"encoding/json"
1212
"fmt"
1313
"io/ioutil"
14+
"math"
1415
"path"
1516
"strconv"
1617
"strings"
@@ -129,6 +130,26 @@ func unescapeUnicode(s, bsonType string) string {
129130
return newS
130131
}
131132

133+
func formatDouble(f float64) string {
134+
var s string
135+
if math.IsInf(f, 1) {
136+
s = "Infinity"
137+
} else if math.IsInf(f, -1) {
138+
s = "-Infinity"
139+
} else if math.IsNaN(f) {
140+
s = "NaN"
141+
} else {
142+
// Print exactly one decimalType place for integers; otherwise, print as many are necessary to
143+
// perfectly represent it.
144+
s = strconv.FormatFloat(f, 'G', -1, 64)
145+
if !strings.ContainsRune(s, 'E') && !strings.ContainsRune(s, '.') {
146+
s += ".0"
147+
}
148+
}
149+
150+
return s
151+
}
152+
132153
func normalizeCanonicalDouble(t *testing.T, key string, cEJ string) string {
133154
// Unmarshal string into map
134155
cEJMap := make(map[string]map[string]string)

bson/bsonrw/extjson_writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ func formatDouble(f float64) string {
621621
// Print exactly one decimalType place for integers; otherwise, print as many are necessary to
622622
// perfectly represent it.
623623
s = strconv.FormatFloat(f, 'G', -1, 64)
624-
if !strings.ContainsRune(s, '.') {
624+
if !strings.ContainsRune(s, 'E') && !strings.ContainsRune(s, '.') {
625625
s += ".0"
626626
}
627627
}

bson/bsonrw/extjson_writer_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,12 @@ func TestExtJSONValueWriter(t *testing.T) {
249249
}
250250
})
251251
})
252+
253+
t.Run("FormatDoubleWithExponent", func(t *testing.T) {
254+
want := "3E-12"
255+
got := formatDouble(float64(0.000000000003))
256+
if got != want {
257+
t.Errorf("Did not receive expected string. got %s: want %s", got, want)
258+
}
259+
})
252260
}

0 commit comments

Comments
 (0)