diff --git a/app.go b/app.go index 48e5b99ac0d..fc80137c517 100644 --- a/app.go +++ b/app.go @@ -318,6 +318,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 decoding + // Default: json.Unmarshal + JSONDecoder utils.JSONUnmarshal `json:"-"` + // XMLEncoder set by an external client of Fiber it will use the provided implementation of a // XMLMarshal // @@ -325,6 +332,13 @@ type Config struct { // Default: xml.Marshal XMLEncoder utils.XMLMarshal `json:"-"` + // XMLDecoder set by an external client of Fiber it will use the provided implementation of a + // XMLUnmarshal + // + // Allowing for flexibility in using another XML library for encoding + // Default: utils.XMLUnmarshal + XMLDecoder utils.XMLUnmarshal `json:"-"` + // App validate. if nil, and context.Validate will always return a error. // Default: nil Validator Validator @@ -510,10 +524,17 @@ 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.XMLEncoder == nil { app.config.XMLEncoder = xml.Marshal } + if app.config.XMLDecoder == nil { + app.config.XMLDecoder = xml.Unmarshal + } + if app.config.Network == "" { app.config.Network = NetworkTCP4 } diff --git a/ctx.go b/ctx.go index e8b9d6fc14d..091c22d8b46 100644 --- a/ctx.go +++ b/ctx.go @@ -284,6 +284,22 @@ func (c *DefaultCtx) Body() []byte { return body } +func (c *DefaultCtx) BodyJSON(v any) error { + if !bytes.HasPrefix(c.Request().Header.ContentType(), utils.UnsafeBytes(MIMEApplicationJSON)) { + return NewError(http.StatusUnsupportedMediaType, "expecting content-type \"application/json\"") + } + + return c.app.config.JSONDecoder(c.Body(), v) +} + +func (c *DefaultCtx) BodyXML(v any) error { + if !bytes.HasPrefix(c.Request().Header.ContentType(), utils.UnsafeBytes(MIMEApplicationXML)) { + return NewError(http.StatusUnsupportedMediaType, "expecting content-type \"application/xml\"") + } + + return c.app.config.XMLDecoder(c.Body(), v) +} + // ClearCookie expires a specific cookie by key on the client side. // If no key is provided it expires all cookies that came with the request. func (c *DefaultCtx) ClearCookie(key ...string) { diff --git a/ctx_interface.go b/ctx_interface.go index 8e554a0290e..35079f4e0a8 100644 --- a/ctx_interface.go +++ b/ctx_interface.go @@ -58,6 +58,12 @@ type Ctx interface { // Make copies or use the Immutable setting instead. Body() []byte + // BodyJSON will check the content-type and unmarshal request body with Config.JSONDecoder + BodyJSON(v any) error + + // BodyXML will check the content-type and unmarshal request body with Config.XMLDecoder + BodyXML(v any) error + // ClearCookie expires a specific cookie by key on the client side. // If no key is provided it expires all cookies that came with the request. ClearCookie(key ...string)