From 2066b01acb8f9daa24a0edd6bf0a4aaed468ee70 Mon Sep 17 00:00:00 2001
From: Tao Wen <taowen@gmail.com>
Date: Tue, 22 Aug 2017 00:12:09 +0800
Subject: [PATCH] #146 support config TagKey

---
 feature_config.go            |  8 ++++++++
 feature_reflect_extension.go |  4 ++--
 feature_reflect_object.go    | 10 +++++-----
 jsoniter_customize_test.go   | 16 ++++++++++++++--
 4 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/feature_config.go b/feature_config.go
index 3c64b6b..980b28d 100644
--- a/feature_config.go
+++ b/feature_config.go
@@ -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)
diff --git a/feature_reflect_extension.go b/feature_reflect_extension.go
index 7321566..3dd3829 100644
--- a/feature_reflect_extension.go
+++ b/feature_reflect_extension.go
@@ -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
diff --git a/feature_reflect_object.go b/feature_reflect_object.go
index 2a4283c..5b891bf 100644
--- a/feature_reflect_object.go
+++ b/feature_reflect_object.go
@@ -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)
 			}
diff --git a/jsoniter_customize_test.go b/jsoniter_customize_test.go
index 80bce63..86237cb 100644
--- a/jsoniter_customize_test.go
+++ b/jsoniter_customize_test.go
@@ -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)
+}
\ No newline at end of file