From 58aeb59006a81e2792cb39298d8eb0734a687cd3 Mon Sep 17 00:00:00 2001 From: AllenX2018 Date: Thu, 26 Mar 2020 14:46:47 +0800 Subject: [PATCH] fix issue #449 --- extra/naming_strategy.go | 2 +- extra/naming_strategy_test.go | 2 ++ reflect_extension.go | 2 +- value_tests/struct_test.go | 5 +++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/extra/naming_strategy.go b/extra/naming_strategy.go index bc4c43ff..916b57d0 100644 --- a/extra/naming_strategy.go +++ b/extra/naming_strategy.go @@ -18,7 +18,7 @@ type namingStrategyExtension struct { func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) { for _, binding := range structDescriptor.Fields { - if unicode.IsLower(rune(binding.Field.Name()[0])) { + if unicode.IsLower(rune(binding.Field.Name()[0])) || binding.Field.Name()[0] == '_'{ continue } tag, hastag := binding.Field.Tag().Lookup("json") diff --git a/extra/naming_strategy_test.go b/extra/naming_strategy_test.go index 29524bff..9d418b37 100644 --- a/extra/naming_strategy_test.go +++ b/extra/naming_strategy_test.go @@ -55,9 +55,11 @@ func Test_set_naming_strategy_with_private_field(t *testing.T) { output, err := jsoniter.Marshal(struct { UserName string userId int + _UserAge int }{ UserName: "allen", userId: 100, + _UserAge: 30, }) should.Nil(err) should.Equal(`{"user_name":"allen"}`, string(output)) diff --git a/reflect_extension.go b/reflect_extension.go index 80320cd6..74a97bfe 100644 --- a/reflect_extension.go +++ b/reflect_extension.go @@ -475,7 +475,7 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, whole fieldNames = []string{tagProvidedFieldName} } // private? - isNotExported := unicode.IsLower(rune(originalFieldName[0])) + isNotExported := unicode.IsLower(rune(originalFieldName[0])) || originalFieldName[0] == '_' if isNotExported { fieldNames = []string{} } diff --git a/value_tests/struct_test.go b/value_tests/struct_test.go index 10ace5c3..7900baae 100644 --- a/value_tests/struct_test.go +++ b/value_tests/struct_test.go @@ -194,6 +194,11 @@ func init() { C: 21, d: time.NewTimer(10 * time.Second), }, + struct { + _UnderscoreField string + }{ + "should not marshal", + }, ) }