ソースを参照

Merge pull request #429 from AllenX2018/fix-typo

fix issue #326
Allen 5 年 前
コミット
69f2a91ff4
2 ファイル変更17 行追加0 行削除
  1. 3 0
      extra/naming_strategy.go
  2. 14 0
      extra/naming_strategy_test.go

+ 3 - 0
extra/naming_strategy.go

@@ -18,6 +18,9 @@ type namingStrategyExtension struct {
 
 func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
 	for _, binding := range structDescriptor.Fields {
+		if unicode.IsLower(rune(binding.Field.Name()[0])) {
+			continue
+		}
 		tag, hastag := binding.Field.Tag().Lookup("json")
 		if hastag {
 			tagParts := strings.Split(tag, ",")

+ 14 - 0
extra/naming_strategy_test.go

@@ -48,3 +48,17 @@ func Test_set_naming_strategy_with_omitempty(t *testing.T) {
 	should.Nil(err)
 	should.Equal(`{"user_name":"taowen"}`, string(output))
 }
+
+func Test_set_naming_strategy_with_private_field(t *testing.T) {
+	should := require.New(t)
+	SetNamingStrategy(LowerCaseWithUnderscores)
+	output, err := jsoniter.Marshal(struct {
+		UserName string
+		userId   int
+	}{
+		UserName: "allen",
+		userId:   100,
+	})
+	should.Nil(err)
+	should.Equal(`{"user_name":"allen"}`, string(output))
+}