Skip to content

Commit

Permalink
fix issue json-iterator#449
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenX2018 committed Mar 26, 2020
1 parent 7d4dbe7 commit 6203136
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion extra/naming_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions extra/naming_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion reflect_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down
5 changes: 5 additions & 0 deletions value_tests/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ func init() {
C: 21,
d: time.NewTimer(10 * time.Second),
},
struct {
_UnderscoreField string
}{
"should not marshal",
},
)
}

Expand Down

0 comments on commit 6203136

Please sign in to comment.