mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-22 20:30:44 +08:00
docs(gin): add custom json codec examples in doc file
This commit is contained in:
parent
c5bd5f7702
commit
0d6990b0c4
60
docs/doc.md
60
docs/doc.md
@ -63,6 +63,7 @@
|
|||||||
- [http2 server push](#http2-server-push)
|
- [http2 server push](#http2-server-push)
|
||||||
- [Define format for the log of routes](#define-format-for-the-log-of-routes)
|
- [Define format for the log of routes](#define-format-for-the-log-of-routes)
|
||||||
- [Set and get a cookie](#set-and-get-a-cookie)
|
- [Set and get a cookie](#set-and-get-a-cookie)
|
||||||
|
- [Custom json codec at runtime](#custom-json-codec-at-runtime)
|
||||||
- [Don't trust all proxies](#dont-trust-all-proxies)
|
- [Don't trust all proxies](#dont-trust-all-proxies)
|
||||||
- [Testing](#testing)
|
- [Testing](#testing)
|
||||||
|
|
||||||
@ -2371,6 +2372,65 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Custom json codec at runtime
|
||||||
|
|
||||||
|
Gin support custom json serialization and deserialization logic without using compile tags.
|
||||||
|
|
||||||
|
1. Define a custom struct implements the `json.Core` interface.
|
||||||
|
|
||||||
|
2. Before your engine starts, assign values to `json.API` using the custom struct.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gin-gonic/gin/codec/json"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
var customConfig = jsoniter.Config{
|
||||||
|
EscapeHTML: true,
|
||||||
|
SortMapKeys: true,
|
||||||
|
ValidateJsonRawMessage: true,
|
||||||
|
}.Froze()
|
||||||
|
|
||||||
|
// implement api.JsonApi
|
||||||
|
type customJsonApi struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j customJsonApi) Marshal(v any) ([]byte, error) {
|
||||||
|
return customConfig.Marshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j customJsonApi) Unmarshal(data []byte, v any) error {
|
||||||
|
return customConfig.Unmarshal(data, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j customJsonApi) MarshalIndent(v any, prefix, indent string) ([]byte, error) {
|
||||||
|
return customConfig.MarshalIndent(v, prefix, indent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j customJsonApi) NewEncoder(writer io.Writer) json.Encoder {
|
||||||
|
return customConfig.NewEncoder(writer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j customJsonApi) NewDecoder(reader io.Reader) json.Decoder {
|
||||||
|
return customConfig.NewDecoder(reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
//Replace the default json api
|
||||||
|
json.API = customJsonApi{}
|
||||||
|
|
||||||
|
//Start your gin engine
|
||||||
|
router := gin.Default()
|
||||||
|
router.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Don't trust all proxies
|
## Don't trust all proxies
|
||||||
|
|
||||||
Gin lets you specify which headers to hold the real client IP (if any),
|
Gin lets you specify which headers to hold the real client IP (if any),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user