1
0
mirror of https://github.com/json-iterator/go.git synced 2025-05-28 22:17:38 +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) {
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")

View File

@ -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))

View File

@ -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{}
}

View File

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