1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-03 22:27:26 +02:00
This commit is contained in:
AllenX2018 2020-03-26 14:46:47 +08:00
parent 7acbb404a4
commit 58aeb59006
4 changed files with 9 additions and 2 deletions

View File

@ -18,7 +18,7 @@ type namingStrategyExtension struct {
func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) { func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
for _, binding := range structDescriptor.Fields { 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 continue
} }
tag, hastag := binding.Field.Tag().Lookup("json") tag, hastag := binding.Field.Tag().Lookup("json")

View File

@ -55,9 +55,11 @@ func Test_set_naming_strategy_with_private_field(t *testing.T) {
output, err := jsoniter.Marshal(struct { output, err := jsoniter.Marshal(struct {
UserName string UserName string
userId int userId int
_UserAge int
}{ }{
UserName: "allen", UserName: "allen",
userId: 100, userId: 100,
_UserAge: 30,
}) })
should.Nil(err) should.Nil(err)
should.Equal(`{"user_name":"allen"}`, string(output)) should.Equal(`{"user_name":"allen"}`, string(output))

View File

@ -475,7 +475,7 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, whole
fieldNames = []string{tagProvidedFieldName} fieldNames = []string{tagProvidedFieldName}
} }
// private? // private?
isNotExported := unicode.IsLower(rune(originalFieldName[0])) isNotExported := unicode.IsLower(rune(originalFieldName[0])) || originalFieldName[0] == '_'
if isNotExported { if isNotExported {
fieldNames = []string{} fieldNames = []string{}
} }

View File

@ -194,6 +194,11 @@ func init() {
C: 21, C: 21,
d: time.NewTimer(10 * time.Second), d: time.NewTimer(10 * time.Second),
}, },
struct {
_UnderscoreField string
}{
"should not marshal",
},
) )
} }