Skip to content

Commit ead5198

Browse files
author
Jason Yellick
committed
[FAB-4317] Fix go 1.7 uint32 parsing in protolator
Under go 1.7, the defaults for JSON unmarshaling are different than for go 1.8. In particular, go 1.7 unmarshals numbers to the float64 type by default, while go 1.8 unmarshals numbers to the json.Numeric type (which stores both an int64 and float64 representation). The float64 numbers cannot be converted to uint32 fields in the proto structures, causing an error in decoding. This CR switches from using the default json.Unmarshal method to a custom json.Decoder to achieve the go 1.8 behavior in go 1.7. Change-Id: I1d3a980f116dddb1749dc9110ffb4a94f2cc105c Signed-off-by: Jason Yellick <[email protected]>
1 parent 3b40efa commit ead5198

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

common/tools/protolator/json.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ func mapToProto(tree map[string]interface{}, msg proto.Message) error {
240240
// and returns it, or error
241241
func jsonToMap(marshaled []byte) (map[string]interface{}, error) {
242242
tree := make(map[string]interface{})
243-
err := json.Unmarshal(marshaled, &tree)
243+
d := json.NewDecoder(bytes.NewReader(marshaled))
244+
d.UseNumber()
245+
err := d.Decode(&tree)
244246
if err != nil {
245247
return nil, fmt.Errorf("error unmarshaling intermediate JSON: %s", err)
246248
}

common/tools/protolator/json_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package protolator
1818

1919
import (
2020
"bytes"
21+
"encoding/json"
2122
"fmt"
23+
"math"
2224
"reflect"
2325
"testing"
2426

@@ -235,3 +237,11 @@ func TestFailFactory(t *testing.T) {
235237
var buffer bytes.Buffer
236238
assert.Error(t, DeepMarshalJSON(&buffer, &testprotos.SimpleMsg{}))
237239
}
240+
241+
func TestJSONUnmarshalMaxUint32(t *testing.T) {
242+
fieldName := "numField"
243+
jsonString := fmt.Sprintf("{\"%s\":%d}", fieldName, math.MaxUint32)
244+
m, err := jsonToMap([]byte(jsonString))
245+
assert.NoError(t, err)
246+
assert.IsType(t, json.Number(""), m[fieldName])
247+
}

0 commit comments

Comments
 (0)