Skip to content

Commit

Permalink
refactor: json config
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Dec 1, 2023
1 parent a327795 commit 7cbbfba
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 10 deletions.
File renamed without changes.
File renamed without changes.
11 changes: 8 additions & 3 deletions pkg/app/server/binding/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
"time"

exprValidator "github.com/bytedance/go-tagexpr/v2/validator"
hJson "github.com/cloudwego/hertz/internal/json"
inDecoder "github.com/cloudwego/hertz/pkg/app/server/binding/internal/decoder"
hJson "github.com/cloudwego/hertz/pkg/common/json"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route/param"
)

type JSONUnmarshaler func(data []byte, v interface{}) error

// BindConfig contains options for default bind behavior.
type BindConfig struct {
// LooseZeroMode if set to true,
Expand Down Expand Up @@ -63,6 +65,8 @@ type BindConfig struct {
// The default is false.
// It is used for BindJSON().
EnableDecoderDisallowUnknownFields bool

JSONUnmarshalFunc JSONUnmarshaler
// TypeUnmarshalFuncs registers customized type unmarshaler.
// NOTE:
// time.Time is registered by default
Expand All @@ -78,6 +82,7 @@ func NewBindConfig() *BindConfig {
DisableStructFieldResolve: false,
EnableDecoderUseNumber: false,
EnableDecoderDisallowUnknownFields: false,
JSONUnmarshalFunc: hJson.Unmarshal,
TypeUnmarshalFuncs: make(map[reflect.Type]inDecoder.CustomizeDecodeFunc),
Validator: defaultValidate,
}
Expand Down Expand Up @@ -127,8 +132,8 @@ func (config *BindConfig) initTypeUnmarshal() {
// NOTE:
//
// UseThirdPartyJSONUnmarshaler will remain in effect once it has been called.
func (config *BindConfig) UseThirdPartyJSONUnmarshaler(fn func(data []byte, v interface{}) error) {
hJson.Unmarshal = fn
func (config *BindConfig) UseThirdPartyJSONUnmarshaler(fn JSONUnmarshaler) {
config.JSONUnmarshalFunc = fn
}

// UseStdJSONUnmarshaler uses encoding/json as json library
Expand Down
8 changes: 6 additions & 2 deletions pkg/app/server/binding/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ import (

exprValidator "github.com/bytedance/go-tagexpr/v2/validator"
"github.com/cloudwego/hertz/internal/bytesconv"
hJson "github.com/cloudwego/hertz/internal/json"
inDecoder "github.com/cloudwego/hertz/pkg/app/server/binding/internal/decoder"
hJson "github.com/cloudwego/hertz/pkg/common/json"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/protocol/consts"
Expand Down Expand Up @@ -325,7 +325,11 @@ func (b *defaultBinder) preBindBody(req *protocol.Request, v interface{}) error
ct := bytesconv.B2s(req.Header.ContentType())
switch utils.FilterContentType(ct) {
case consts.MIMEApplicationJSON:
return hJson.Unmarshal(req.Body(), v)
fn := b.config.JSONUnmarshalFunc
if fn == nil {
fn = hJson.Unmarshal
}
return fn(req.Body(), v)
case consts.MIMEPROTOBUF:
msg, ok := v.(proto.Message)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"reflect"

"github.com/cloudwego/hertz/internal/bytesconv"
hJson "github.com/cloudwego/hertz/pkg/common/json"
hJson "github.com/cloudwego/hertz/internal/json"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route/param"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
"reflect"

"github.com/cloudwego/hertz/internal/bytesconv"
hJson "github.com/cloudwego/hertz/pkg/common/json"
hJson "github.com/cloudwego/hertz/internal/json"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route/param"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"reflect"

"github.com/cloudwego/hertz/internal/bytesconv"
hjson "github.com/cloudwego/hertz/internal/json"
"github.com/cloudwego/hertz/pkg/common/hlog"
hjson "github.com/cloudwego/hertz/pkg/common/json"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route/param"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/server/binding/internal/decoder/text_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
"strconv"

"github.com/cloudwego/hertz/internal/bytesconv"
hJson "github.com/cloudwego/hertz/pkg/common/json"
hJson "github.com/cloudwego/hertz/internal/json"
)

type TextDecoder interface {
Expand Down
4 changes: 3 additions & 1 deletion pkg/app/server/render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"bytes"
"encoding/json"

hjson "github.com/cloudwego/hertz/pkg/common/json"
hjson "github.com/cloudwego/hertz/internal/json"
"github.com/cloudwego/hertz/pkg/protocol"
)

Expand All @@ -58,10 +58,12 @@ func init() {
ResetJSONMarshal(hjson.Marshal)
}

// Deprecated:
func ResetJSONMarshal(fn JSONMarshaler) {
jsonMarshalFunc = fn
}

// Deprecated:
func ResetStdJSONMarshal() {
ResetJSONMarshal(json.Marshal)
}
Expand Down

0 comments on commit 7cbbfba

Please sign in to comment.