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