增加json的请求Body解析
This commit is contained in:
@ -6,3 +6,44 @@
|
||||
//
|
||||
// Date : 2024-10-22 16:42
|
||||
package parse_body
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type JsonAdaptor struct {
|
||||
base
|
||||
}
|
||||
|
||||
// Parse 解析json请求体
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:13 2024/10/22
|
||||
func (j JsonAdaptor) Parse(ctx *gin.Context, receiver any) ([]byte, error) {
|
||||
unmarshalFunc := j.Unmarshal()
|
||||
if nil == unmarshalFunc {
|
||||
unmarshalFunc = serialize.JSON.UnmarshalWithNumber
|
||||
}
|
||||
return j.DoParse(ctx, receiver, unmarshalFunc)
|
||||
}
|
||||
|
||||
// Unmarshal 指定解析方法
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:56 2024/10/22
|
||||
func (j JsonAdaptor) Unmarshal() func(sourceData []byte, receiver any) error {
|
||||
return serialize.JSON.UnmarshalWithNumber
|
||||
}
|
||||
|
||||
// ContentType 请求类型固定返回json
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:59 2024/10/22
|
||||
func (j JsonAdaptor) ContentType() string {
|
||||
return consts.MimeTypeJson
|
||||
}
|
||||
|
Reference in New Issue
Block a user