mirror of
https://github.com/json-iterator/go.git
synced 2025-05-13 21:36:29 +02:00
#146 support config TagKey
This commit is contained in:
parent
ac3b3cd160
commit
2066b01acb
@ -17,6 +17,7 @@ type Config struct {
|
||||
EscapeHTML bool
|
||||
SortMapKeys bool
|
||||
UseNumber bool
|
||||
TagKey string
|
||||
}
|
||||
|
||||
type frozenConfig struct {
|
||||
@ -95,6 +96,13 @@ func (cfg *frozenConfig) useNumber() {
|
||||
}
|
||||
}})
|
||||
}
|
||||
func (cfg *frozenConfig) getTagKey() string {
|
||||
tagKey := cfg.configBeforeFrozen.TagKey
|
||||
if tagKey == "" {
|
||||
return "json"
|
||||
}
|
||||
return tagKey
|
||||
}
|
||||
|
||||
func (cfg *frozenConfig) registerExtension(extension Extension) {
|
||||
cfg.extensions = append(cfg.extensions, extension)
|
||||
|
@ -227,7 +227,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
||||
bindings := []*Binding{}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
tag := field.Tag.Get("json")
|
||||
tag := field.Tag.Get(cfg.getTagKey())
|
||||
tagParts := strings.Split(tag, ",")
|
||||
if tag == "-" {
|
||||
continue
|
||||
@ -373,7 +373,7 @@ func (bindings sortableBindings) Swap(i, j int) {
|
||||
func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
shouldOmitEmpty := false
|
||||
tagParts := strings.Split(binding.Field.Tag.Get("json"), ",")
|
||||
tagParts := strings.Split(binding.Field.Tag.Get(cfg.getTagKey()), ",")
|
||||
for _, tagPart := range tagParts[1:] {
|
||||
if tagPart == "omitempty" {
|
||||
shouldOmitEmpty = true
|
||||
|
@ -29,7 +29,7 @@ func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||
if old.toName != toName {
|
||||
continue
|
||||
}
|
||||
old.ignored, new.ignored = resolveConflictBinding(old.binding, new.binding)
|
||||
old.ignored, new.ignored = resolveConflictBinding(cfg, old.binding, new.binding)
|
||||
}
|
||||
orderedBindings = append(orderedBindings, new)
|
||||
}
|
||||
@ -49,9 +49,9 @@ func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
||||
return &structEncoder{structDescriptor.onePtrEmbedded, structDescriptor.onePtrOptimization, finalOrderedFields}, nil
|
||||
}
|
||||
|
||||
func resolveConflictBinding(old, new *Binding) (ignoreOld, ignoreNew bool) {
|
||||
newTagged := new.Field.Tag.Get("json") != ""
|
||||
oldTagged := old.Field.Tag.Get("json") != ""
|
||||
func resolveConflictBinding(cfg *frozenConfig, old, new *Binding) (ignoreOld, ignoreNew bool) {
|
||||
newTagged := new.Field.Tag.Get(cfg.getTagKey()) != ""
|
||||
oldTagged := old.Field.Tag.Get(cfg.getTagKey()) != ""
|
||||
if newTagged {
|
||||
if oldTagged {
|
||||
if len(old.levels) > len(new.levels) {
|
||||
@ -91,7 +91,7 @@ func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
||||
bindings[fromName] = binding
|
||||
continue
|
||||
}
|
||||
ignoreOld, ignoreNew := resolveConflictBinding(old, binding)
|
||||
ignoreOld, ignoreNew := resolveConflictBinding(cfg, old, binding)
|
||||
if ignoreOld {
|
||||
delete(bindings, fromName)
|
||||
}
|
||||
|
@ -7,8 +7,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func Test_customize_type_decoder(t *testing.T) {
|
||||
@ -307,3 +305,17 @@ func Test_unmarshal_empty_interface_as_int64(t *testing.T) {
|
||||
Unmarshal([]byte("[100]"), &arr)
|
||||
should.Equal(int64(100), arr[0])
|
||||
}
|
||||
|
||||
|
||||
func Test_customize_tag_key(t *testing.T) {
|
||||
|
||||
type TestObject struct {
|
||||
Field string `orm:"field"`
|
||||
}
|
||||
|
||||
should := require.New(t)
|
||||
json := Config{TagKey: "orm"}.Froze()
|
||||
str, err := json.MarshalToString(TestObject{"hello"})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"field":"hello"}`, str)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user