JSON marshals the given "v" value to JSON and writes the response to the client. Look the Configuration.EnableProtoJSON and EnableEasyJSON too. It reports any JSON parser or write errors back to the caller. Look the Application.SetContextErrorHandler to override the default status code 500 with a c
(v interface{}, opts ...JSON)
| 4344 | // Customize the behavior of every `Context.JSON“ can be achieved |
| 4345 | // by modifying the package-level `WriteJSON` function on program initilization. |
| 4346 | func (ctx *Context) JSON(v interface{}, opts ...JSON) (err error) { |
| 4347 | var options *JSON |
| 4348 | if len(opts) > 0 { |
| 4349 | options = &opts[0] |
| 4350 | } else { |
| 4351 | // If no options are given safely read the already-initialized value. |
| 4352 | options = &DefaultJSONOptions |
| 4353 | } |
| 4354 | |
| 4355 | if err = ctx.writeJSON(v, options); err != nil { |
| 4356 | // if no options are given or OmitErrorHandler is false |
| 4357 | // then call the error handler (which may lead to a cycle if the error handler fails to write JSON too). |
| 4358 | if !options.OmitErrorHandler { |
| 4359 | ctx.handleContextError(err) |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | return |
| 4364 | } |
| 4365 | |
| 4366 | func (ctx *Context) writeJSON(v interface{}, options *JSON) error { |
| 4367 | ctx.ContentType(ContentJSONHeaderValue) |
no test coverage detected