Skip to content

Commit 58aeb59

Browse files
committed
fix issue #449
1 parent 7acbb40 commit 58aeb59

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

extra/naming_strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type namingStrategyExtension struct {
1818

1919
func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
2020
for _, binding := range structDescriptor.Fields {
21-
if unicode.IsLower(rune(binding.Field.Name()[0])) {
21+
if unicode.IsLower(rune(binding.Field.Name()[0])) || binding.Field.Name()[0] == '_'{
2222
continue
2323
}
2424
tag, hastag := binding.Field.Tag().Lookup("json")

extra/naming_strategy_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ func Test_set_naming_strategy_with_private_field(t *testing.T) {
5555
output, err := jsoniter.Marshal(struct {
5656
UserName string
5757
userId int
58+
_UserAge int
5859
}{
5960
UserName: "allen",
6061
userId: 100,
62+
_UserAge: 30,
6163
})
6264
should.Nil(err)
6365
should.Equal(`{"user_name":"allen"}`, string(output))

reflect_extension.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, whole
475475
fieldNames = []string{tagProvidedFieldName}
476476
}
477477
// private?
478-
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
478+
isNotExported := unicode.IsLower(rune(originalFieldName[0])) || originalFieldName[0] == '_'
479479
if isNotExported {
480480
fieldNames = []string{}
481481
}

value_tests/struct_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ func init() {
194194
C: 21,
195195
d: time.NewTimer(10 * time.Second),
196196
},
197+
struct {
198+
_UnderscoreField string
199+
}{
200+
"should not marshal",
201+
},
197202
)
198203
}
199204

0 commit comments

Comments
 (0)