1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-23 11:37:32 +02:00

config: add OnlyTaggedField config, only process tagged fields in struct

This commit is contained in:
李盼 2018-01-09 17:29:47 +08:00
parent 0ab880662f
commit c27f6f9350
2 changed files with 7 additions and 1 deletions

View File

@ -18,6 +18,7 @@ type Config struct {
SortMapKeys bool SortMapKeys bool
UseNumber bool UseNumber bool
TagKey string TagKey string
OnlyTaggedField bool
ValidateJsonRawMessage bool ValidateJsonRawMessage bool
ObjectFieldMustBeSimpleString bool ObjectFieldMustBeSimpleString bool
} }
@ -27,6 +28,7 @@ type frozenConfig struct {
sortMapKeys bool sortMapKeys bool
indentionStep int indentionStep int
objectFieldMustBeSimpleString bool objectFieldMustBeSimpleString bool
onlyTaggedField bool
decoderCache unsafe.Pointer decoderCache unsafe.Pointer
encoderCache unsafe.Pointer encoderCache unsafe.Pointer
extensions []Extension extensions []Extension
@ -77,6 +79,7 @@ func (cfg Config) Froze() API {
sortMapKeys: cfg.SortMapKeys, sortMapKeys: cfg.SortMapKeys,
indentionStep: cfg.IndentionStep, indentionStep: cfg.IndentionStep,
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString, objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
onlyTaggedField: cfg.OnlyTaggedField,
streamPool: make(chan *Stream, 16), streamPool: make(chan *Stream, 16),
iteratorPool: make(chan *Iterator, 16), iteratorPool: make(chan *Iterator, 16),
} }

View File

@ -245,7 +245,10 @@ func describeStruct(cfg *frozenConfig, prefix string, typ reflect.Type) *StructD
bindings := []*Binding{} bindings := []*Binding{}
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i) field := typ.Field(i)
tag := field.Tag.Get(cfg.getTagKey()) tag, hastag := field.Tag.Lookup(cfg.getTagKey())
if cfg.onlyTaggedField && !hastag {
continue
}
tagParts := strings.Split(tag, ",") tagParts := strings.Split(tag, ",")
if tag == "-" { if tag == "-" {
continue continue