Skip to content

Commit

Permalink
CopyToJSON: fix bitSize for floats
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorj authored and philhofer committed Sep 17, 2020
1 parent e88e92c commit 87c1ec4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions msgp/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func rwFloat32(dst jsWriter, src *Reader) (int, error) {
if err != nil {
return 0, err
}
src.scratch = strconv.AppendFloat(src.scratch[:0], float64(f), 'f', -1, 64)
src.scratch = strconv.AppendFloat(src.scratch[:0], float64(f), 'f', -1, 32)
return dst.Write(src.scratch)
}

Expand All @@ -215,7 +215,7 @@ func rwFloat64(dst jsWriter, src *Reader) (int, error) {
if err != nil {
return 0, err
}
src.scratch = strconv.AppendFloat(src.scratch[:0], f, 'f', -1, 32)
src.scratch = strconv.AppendFloat(src.scratch[:0], f, 'f', -1, 64)
return dst.Write(src.scratch)
}

Expand Down
13 changes: 10 additions & 3 deletions msgp/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
func TestCopyJSON(t *testing.T) {
var buf bytes.Buffer
enc := NewWriter(&buf)
enc.WriteMapHeader(5)
const mapLength = 6
enc.WriteMapHeader(mapLength)

enc.WriteString("thing_1")
enc.WriteString("a string object")
Expand All @@ -33,6 +34,9 @@ func TestCopyJSON(t *testing.T) {
"internal_one": "blah",
"internal_two": "blahhh...",
})
enc.WriteString("float64")
const encodedFloat64 = 1672209023
enc.WriteFloat64(encodedFloat64)
enc.Flush()

var js bytes.Buffer
Expand All @@ -47,8 +51,8 @@ func TestCopyJSON(t *testing.T) {
t.Fatalf("Error unmarshaling: %s", err)
}

if len(mp) != 5 {
t.Errorf("map length should be %d, not %d", 4, len(mp))
if len(mp) != mapLength {
t.Errorf("map length should be %d, not %d", mapLength, len(mp))
}

so, ok := mp["thing_1"]
Expand All @@ -68,6 +72,9 @@ func TestCopyJSON(t *testing.T) {
t.Errorf("inner map field %q should be %q, not %q", "internal_one", "blah", inm1)
}
}
if actual := mp["float64"]; float64(encodedFloat64) != actual.(float64) {
t.Errorf("expected %G, got %G", float64(encodedFloat64), actual)
}
}

// Encoder should generate valid utf-8 even if passed bad input
Expand Down

0 comments on commit 87c1ec4

Please sign in to comment.