Skip to content

Commit

Permalink
πŸ“ Update: Add JSONDecoder to config (#1489)
Browse files Browse the repository at this point in the history
* πŸ“ Update: Add JSONDecoder to config
  • Loading branch information
pd-pranay committed Aug 18, 2021
1 parent 385c94a commit 670170f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 670170f

Please sign in to comment.