Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit f41e93f

Browse files
authored
Merge pull request #261 from onlyice/feature/json-number-to-uint64
Add support for parsing json.Number to uint64
2 parents 5ac1f6a + ea0720b commit f41e93f

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

mapstructure.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,12 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
684684
}
685685
case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number":
686686
jn := data.(json.Number)
687-
i, err := jn.Int64()
687+
i, err := strconv.ParseUint(string(jn), 0, 64)
688688
if err != nil {
689689
return fmt.Errorf(
690690
"error decoding json.Number into %s: %s", name, err)
691691
}
692-
if i < 0 && !d.config.WeaklyTypedInput {
693-
return fmt.Errorf("cannot parse '%s', %d overflows uint",
694-
name, i)
695-
}
696-
val.SetUint(uint64(i))
692+
val.SetUint(i)
697693
default:
698694
return fmt.Errorf(
699695
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",

mapstructure_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Basic struct {
2424
Vdata interface{}
2525
VjsonInt int
2626
VjsonUint uint
27+
VjsonUint64 uint64
2728
VjsonFloat float64
2829
VjsonNumber json.Number
2930
}
@@ -224,6 +225,7 @@ func TestBasicTypes(t *testing.T) {
224225
"vdata": 42,
225226
"vjsonInt": json.Number("1234"),
226227
"vjsonUint": json.Number("1234"),
228+
"vjsonUint64": json.Number("9223372036854775809"), // 2^63 + 1
227229
"vjsonFloat": json.Number("1234.5"),
228230
"vjsonNumber": json.Number("1234.5"),
229231
}
@@ -287,6 +289,10 @@ func TestBasicTypes(t *testing.T) {
287289
t.Errorf("vjsonuint value should be 1234: %#v", result.VjsonUint)
288290
}
289291

292+
if result.VjsonUint64 != 9223372036854775809 {
293+
t.Errorf("vjsonuint64 value should be 9223372036854775809: %#v", result.VjsonUint64)
294+
}
295+
290296
if result.VjsonFloat != 1234.5 {
291297
t.Errorf("vjsonfloat value should be 1234.5: %#v", result.VjsonFloat)
292298
}
@@ -1721,6 +1727,7 @@ func TestDecodeTable(t *testing.T) {
17211727
"Vdata": []byte("data"),
17221728
"VjsonInt": 0,
17231729
"VjsonUint": uint(0),
1730+
"VjsonUint64": uint64(0),
17241731
"VjsonFloat": 0.0,
17251732
"VjsonNumber": json.Number(""),
17261733
},
@@ -1762,6 +1769,7 @@ func TestDecodeTable(t *testing.T) {
17621769
"Vdata": []byte("data"),
17631770
"VjsonInt": 0,
17641771
"VjsonUint": uint(0),
1772+
"VjsonUint64": uint64(0),
17651773
"VjsonFloat": 0.0,
17661774
"VjsonNumber": json.Number(""),
17671775
},

0 commit comments

Comments
 (0)