diff --git a/decode.go b/decode.go index 3d31096..eedbe05 100644 --- a/decode.go +++ b/decode.go @@ -24,7 +24,7 @@ type emptyInterface struct { ptr unsafe.Pointer } -func unmarshal(data []byte, v interface{}) error { +func unmarshal(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error { src := make([]byte, len(data)+1) // append nul byte to the end copy(src, data) diff --git a/internal/decoder/option.go b/internal/decoder/option.go new file mode 100644 index 0000000..a44ca55 --- /dev/null +++ b/internal/decoder/option.go @@ -0,0 +1,5 @@ +package decoder + +type Option struct { + FirstWin bool +} diff --git a/json.go b/json.go index 4ac8536..2517368 100644 --- a/json.go +++ b/json.go @@ -258,6 +258,10 @@ func Unmarshal(data []byte, v interface{}) error { return unmarshal(data, v) } +func UnmarshalWithOption(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error { + return unmarshal(data, v, optFuncs...) +} + func UnmarshalNoEscape(data []byte, v interface{}) error { return unmarshalNoEscape(data, v) } diff --git a/option.go b/option.go index 444ffd0..d2cd3b1 100644 --- a/option.go +++ b/option.go @@ -1,11 +1,11 @@ package json import ( + "github.com/goccy/go-json/internal/decoder" "github.com/goccy/go-json/internal/encoder" ) type EncodeOption = encoder.Option - type EncodeOptionFunc func(*EncodeOption) func UnorderedMap() EncodeOptionFunc { @@ -26,3 +26,12 @@ func Colorize(scheme *ColorScheme) EncodeOptionFunc { opt.ColorScheme = scheme } } + +type DecodeOption = decoder.Option +type DecodeOptionFunc func(*DecodeOption) + +func DecodeFieldPriorityFirstWin() DecodeOptionFunc { + return func(opt *DecodeOption) { + opt.FirstWin = true + } +}