2015-03-31 17:51:10 +02:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-07-08 16:49:09 +08:00
|
|
|
|
2017-07-18 16:01:29 -05:00
|
|
|
"github.com/gin-gonic/gin/json"
|
2015-03-31 17:51:10 +02:00
|
|
|
)
|
|
|
|
|
2017-07-10 17:33:35 +09:00
|
|
|
var (
|
|
|
|
EnableDecoderUseNumber = false
|
|
|
|
)
|
2017-07-08 18:19:09 +08:00
|
|
|
|
2015-03-31 17:51:10 +02:00
|
|
|
type jsonBinding struct{}
|
|
|
|
|
2015-07-10 13:06:01 +02:00
|
|
|
func (jsonBinding) Name() string {
|
2015-03-31 17:51:10 +02:00
|
|
|
return "json"
|
|
|
|
}
|
|
|
|
|
2015-07-10 13:06:01 +02:00
|
|
|
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
|
2015-03-31 17:51:10 +02:00
|
|
|
decoder := json.NewDecoder(req.Body)
|
2017-07-10 17:33:35 +09:00
|
|
|
if EnableDecoderUseNumber {
|
|
|
|
decoder.UseNumber()
|
|
|
|
}
|
2015-04-07 12:30:16 +02:00
|
|
|
if err := decoder.Decode(obj); err != nil {
|
2015-03-31 17:51:10 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-05-31 16:30:00 +02:00
|
|
|
return validate(obj)
|
2015-03-31 17:51:10 +02:00
|
|
|
}
|