* resolve #368 add option to UseNumber() in gin.Context.BindJSON() * add test
This commit is contained in:
parent
b6256dbe0c
commit
fb7448f081
@ -10,7 +10,10 @@ import (
|
|||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
var (
|
||||||
|
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
EnableDecoderUseNumber = false
|
||||||
|
)
|
||||||
|
|
||||||
type jsonBinding struct{}
|
type jsonBinding struct{}
|
||||||
|
|
||||||
@ -20,6 +23,9 @@ func (jsonBinding) Name() string {
|
|||||||
|
|
||||||
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
||||||
decoder := json.NewDecoder(req.Body)
|
decoder := json.NewDecoder(req.Body)
|
||||||
|
if EnableDecoderUseNumber {
|
||||||
|
decoder.UseNumber()
|
||||||
|
}
|
||||||
if err := decoder.Decode(obj); err != nil {
|
if err := decoder.Decode(obj); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
4
mode.go
4
mode.go
@ -64,6 +64,10 @@ func DisableBindValidation() {
|
|||||||
binding.Validator = nil
|
binding.Validator = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EnableJsonDecoderUseNumber() {
|
||||||
|
binding.EnableDecoderUseNumber = true
|
||||||
|
}
|
||||||
|
|
||||||
func Mode() string {
|
func Mode() string {
|
||||||
return modeName
|
return modeName
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,3 +35,9 @@ func TestSetMode(t *testing.T) {
|
|||||||
|
|
||||||
assert.Panics(t, func() { SetMode("unknown") })
|
assert.Panics(t, func() { SetMode("unknown") })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnableJsonDecoderUseNumber(t *testing.T) {
|
||||||
|
assert.False(t, binding.EnableDecoderUseNumber)
|
||||||
|
EnableJsonDecoderUseNumber()
|
||||||
|
assert.True(t, binding.EnableDecoderUseNumber)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user