Fix #216: Enable to call binding multiple times in some formats (#1341)

* Add interface to read body bytes in binding

* Add BindingBody implementation for some binding

* Fix to use `BindBodyBytesKey` for key

* Revert "Fix to use `BindBodyBytesKey` for key"

This reverts commit 2c82901ceab6ae53730a3cfcd9839bee11a08f13.

* Use private-like key for body bytes

* Add tests for BindingBody & ShouldBindBodyWith

* Add note for README

* Remove redundant space between sentences
This commit is contained in:
JINNOUCHI Yasushi
2018-05-11 11:33:33 +09:00
committed by Bo-Yi Wu
parent 6e09ef03b0
commit 995fa8e9ce
9 changed files with 279 additions and 10 deletions

View File

@ -5,6 +5,8 @@
package binding
import (
"bytes"
"io"
"net/http"
"github.com/gin-gonic/gin/json"
@ -22,7 +24,15 @@ func (jsonBinding) Name() string {
}
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
decoder := json.NewDecoder(req.Body)
return decodeJSON(req.Body, obj)
}
func (jsonBinding) BindBody(body []byte, obj interface{}) error {
return decodeJSON(bytes.NewReader(body), obj)
}
func decodeJSON(r io.Reader, obj interface{}) error {
decoder := json.NewDecoder(r)
if EnableDecoderUseNumber {
decoder.UseNumber()
}