📝 Update: Add JSONDecoder to config (#1489)

* 📝 Update: Add JSONDecoder to config
pull/1501/head
Pranay 2021-08-18 17:56:07 +05:30 committed by GitHub
parent 385c94adcd
commit 670170f7c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

10
app.go
View File

@ -308,6 +308,13 @@ type Config struct {
// Default: json.Marshal
JSONEncoder utils.JSONMarshal `json:"-"`
// When set by an external client of Fiber it will use the provided implementation of a
// JSONUnmarshal
//
// Allowing for flexibility in using another json library for encoding
// Default: json.Unmarshal
JSONDecoder utils.JSONUnmarshal `json:"-"`
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
// WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chose.
//
@ -459,6 +466,9 @@ func New(config ...Config) *App {
if app.config.JSONEncoder == nil {
app.config.JSONEncoder = json.Marshal
}
if app.config.JSONDecoder == nil {
app.config.JSONDecoder = json.Unmarshal
}
if app.config.Network == "" {
app.config.Network = NetworkTCP4
}

2
ctx.go
View File

@ -291,7 +291,7 @@ func (c *Ctx) BodyParser(out interface{}) error {
// Parse body accordingly
if strings.HasPrefix(ctype, MIMEApplicationJSON) {
schemaDecoder.SetAliasTag("json")
return json.Unmarshal(c.Body(), out)
return c.app.config.JSONDecoder(c.Body(), out)
}
if strings.HasPrefix(ctype, MIMEApplicationForm) {
schemaDecoder.SetAliasTag("form")