mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Context.JSONOptions: add usage godoc example
This commit is contained in:
parent
84d1a60334
commit
4f81478ad6
|
@ -3781,8 +3781,8 @@ func (j *JSON) IsDefault() bool {
|
||||||
j.Prefix == DefaultJSONOptions.Prefix &&
|
j.Prefix == DefaultJSONOptions.Prefix &&
|
||||||
j.ASCII == DefaultJSONOptions.ASCII &&
|
j.ASCII == DefaultJSONOptions.ASCII &&
|
||||||
j.Secure == DefaultJSONOptions.Secure &&
|
j.Secure == DefaultJSONOptions.Secure &&
|
||||||
j.Proto == DefaultJSONOptions.Proto &&
|
j.Proto == DefaultJSONOptions.Proto
|
||||||
j.ErrorHandler == nil
|
// except context and error handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetContext returns the option's Context or the HTTP request's one.
|
// GetContext returns the option's Context or the HTTP request's one.
|
||||||
|
@ -3958,6 +3958,20 @@ const jsonOptionsContextKey = "iris.context.json_options"
|
||||||
// SetJSONOptions stores the given JSON options to the handler
|
// SetJSONOptions stores the given JSON options to the handler
|
||||||
// for any next Context.JSON calls. Note that the Context.JSON's
|
// for any next Context.JSON calls. Note that the Context.JSON's
|
||||||
// variadic options have priority over these given options.
|
// variadic options have priority over these given options.
|
||||||
|
//
|
||||||
|
// Usage Example:
|
||||||
|
//
|
||||||
|
// type jsonErrorHandler struct{}
|
||||||
|
// func (e *jsonErrorHandler) HandleContextError(ctx iris.Context, err error) {
|
||||||
|
// errors.InvalidArgument.Err(ctx, err)
|
||||||
|
// }
|
||||||
|
// ...
|
||||||
|
// errHandler := new(jsonErrorHandler)
|
||||||
|
// srv.Use(func(ctx iris.Context) {
|
||||||
|
// ctx.SetJSONOptions(iris.JSON{
|
||||||
|
// ErrorHandler: errHandler,
|
||||||
|
// })
|
||||||
|
// })
|
||||||
func (ctx *Context) SetJSONOptions(opts JSON) {
|
func (ctx *Context) SetJSONOptions(opts JSON) {
|
||||||
ctx.values.Set(jsonOptionsContextKey, opts)
|
ctx.values.Set(jsonOptionsContextKey, opts)
|
||||||
}
|
}
|
||||||
|
@ -3994,10 +4008,12 @@ func (ctx *Context) writeJSON(v interface{}, opts ...JSON) (n int, err error) {
|
||||||
options = opts[0]
|
options = opts[0]
|
||||||
} else {
|
} else {
|
||||||
if opt, ok := ctx.getJSONOptions(); ok {
|
if opt, ok := ctx.getJSONOptions(); ok {
|
||||||
opts = []JSON{opt}
|
options = opt
|
||||||
|
if !options.IsDefault() { // keep the next branch valid when only the Context or/and ErrorHandler are modified.
|
||||||
optsLength = 1
|
optsLength = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if shouldOptimize && optsLength == 0 { // if no options given and optimizations are enabled.
|
if shouldOptimize && optsLength == 0 { // if no options given and optimizations are enabled.
|
||||||
// try handle proto or easyjson.
|
// try handle proto or easyjson.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user