Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 2066b01

Browse files
committed
#146 support config TagKey
1 parent ac3b3cd commit 2066b01

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

feature_config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Config struct {
1717
EscapeHTML bool
1818
SortMapKeys bool
1919
UseNumber bool
20+
TagKey string
2021
}
2122

2223
type frozenConfig struct {
@@ -95,6 +96,13 @@ func (cfg *frozenConfig) useNumber() {
9596
}
9697
}})
9798
}
99+
func (cfg *frozenConfig) getTagKey() string {
100+
tagKey := cfg.configBeforeFrozen.TagKey
101+
if tagKey == "" {
102+
return "json"
103+
}
104+
return tagKey
105+
}
98106

99107
func (cfg *frozenConfig) registerExtension(extension Extension) {
100108
cfg.extensions = append(cfg.extensions, extension)

feature_reflect_extension.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
227227
bindings := []*Binding{}
228228
for i := 0; i < typ.NumField(); i++ {
229229
field := typ.Field(i)
230-
tag := field.Tag.Get("json")
230+
tag := field.Tag.Get(cfg.getTagKey())
231231
tagParts := strings.Split(tag, ",")
232232
if tag == "-" {
233233
continue
@@ -373,7 +373,7 @@ func (bindings sortableBindings) Swap(i, j int) {
373373
func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
374374
for _, binding := range structDescriptor.Fields {
375375
shouldOmitEmpty := false
376-
tagParts := strings.Split(binding.Field.Tag.Get("json"), ",")
376+
tagParts := strings.Split(binding.Field.Tag.Get(cfg.getTagKey()), ",")
377377
for _, tagPart := range tagParts[1:] {
378378
if tagPart == "omitempty" {
379379
shouldOmitEmpty = true

feature_reflect_object.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
2929
if old.toName != toName {
3030
continue
3131
}
32-
old.ignored, new.ignored = resolveConflictBinding(old.binding, new.binding)
32+
old.ignored, new.ignored = resolveConflictBinding(cfg, old.binding, new.binding)
3333
}
3434
orderedBindings = append(orderedBindings, new)
3535
}
@@ -49,9 +49,9 @@ func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
4949
return &structEncoder{structDescriptor.onePtrEmbedded, structDescriptor.onePtrOptimization, finalOrderedFields}, nil
5050
}
5151

52-
func resolveConflictBinding(old, new *Binding) (ignoreOld, ignoreNew bool) {
53-
newTagged := new.Field.Tag.Get("json") != ""
54-
oldTagged := old.Field.Tag.Get("json") != ""
52+
func resolveConflictBinding(cfg *frozenConfig, old, new *Binding) (ignoreOld, ignoreNew bool) {
53+
newTagged := new.Field.Tag.Get(cfg.getTagKey()) != ""
54+
oldTagged := old.Field.Tag.Get(cfg.getTagKey()) != ""
5555
if newTagged {
5656
if oldTagged {
5757
if len(old.levels) > len(new.levels) {
@@ -91,7 +91,7 @@ func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
9191
bindings[fromName] = binding
9292
continue
9393
}
94-
ignoreOld, ignoreNew := resolveConflictBinding(old, binding)
94+
ignoreOld, ignoreNew := resolveConflictBinding(cfg, old, binding)
9595
if ignoreOld {
9696
delete(bindings, fromName)
9797
}

jsoniter_customize_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"testing"
88
"time"
99
"unsafe"
10-
"fmt"
11-
"reflect"
1210
)
1311

1412
func Test_customize_type_decoder(t *testing.T) {
@@ -307,3 +305,17 @@ func Test_unmarshal_empty_interface_as_int64(t *testing.T) {
307305
Unmarshal([]byte("[100]"), &arr)
308306
should.Equal(int64(100), arr[0])
309307
}
308+
309+
310+
func Test_customize_tag_key(t *testing.T) {
311+
312+
type TestObject struct {
313+
Field string `orm:"field"`
314+
}
315+
316+
should := require.New(t)
317+
json := Config{TagKey: "orm"}.Froze()
318+
str, err := json.MarshalToString(TestObject{"hello"})
319+
should.Nil(err)
320+
should.Equal(`{"field":"hello"}`, str)
321+
}

0 commit comments

Comments
 (0)