Skip to content

Commit

Permalink
Allow hanlding of strings that wrap a number to be deserialized to int64
Browse files Browse the repository at this point in the history
Fixes issue #5000
  • Loading branch information
liranms committed Sep 27, 2023
1 parent 91e36ec commit aa1606d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions private/protocol/json/jsonutil/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"math/big"
"reflect"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -259,6 +260,16 @@ func (u unmarshaler) unmarshalScalar(value reflect.Value, data interface{}, tag
return err
}
value.Set(reflect.ValueOf(v))
case *int64:
v, err := strconv.ParseInt(d, 10, 64)
if err != nil {
fp, err := strconv.ParseFloat(d, 64)
if err != nil {
return fmt.Errorf("failed converting string that should contain a number to int64 %s: %w", d, err)
}
v = int64(fp)
}
value.Set(reflect.ValueOf(aws.Int64(v)))
case *float64:
// These are regular strings when parsed by encoding/json's unmarshaler.
switch {
Expand Down
12 changes: 12 additions & 0 deletions private/protocol/json/jsonutil/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ func TestUnmarshalJSON_JSONNumber(t *testing.T) {
IntField: aws.Int64(123456789),
},
},
"integer field as string": {
JSON: `{"intField":"123456789"}`,
Expected: input{
IntField: aws.Int64(123456789),
},
},
"integer field as string truncated": {
JSON: `{"intField":"123456789.123"}`,
Expected: input{
IntField: aws.Int64(123456789),
},
},
"float64 field": {
JSON: `{"floatField":123456789.123}`,
Expected: input{
Expand Down

0 comments on commit aa1606d

Please sign in to comment.