1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00
This commit is contained in:
allen 2020-01-10 16:11:04 +08:00
parent 9c0685d8d3
commit 78d9e97b7a
2 changed files with 17 additions and 0 deletions

View File

@ -18,6 +18,9 @@ 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])) {
continue
}
tag, hastag := binding.Field.Tag().Lookup("json") tag, hastag := binding.Field.Tag().Lookup("json")
if hastag { if hastag {
tagParts := strings.Split(tag, ",") tagParts := strings.Split(tag, ",")

View File

@ -48,3 +48,17 @@ func Test_set_naming_strategy_with_omitempty(t *testing.T) {
should.Nil(err) should.Nil(err)
should.Equal(`{"user_name":"taowen"}`, string(output)) should.Equal(`{"user_name":"taowen"}`, string(output))
} }
func Test_set_naming_strategy_with_private_field(t *testing.T) {
should := require.New(t)
SetNamingStrategy(LowerCaseWithUnderscores)
output, err := jsoniter.Marshal(struct {
UserName string
userId int
}{
UserName: "allen",
userId: 100,
})
should.Nil(err)
should.Equal(`{"user_name":"allen"}`, string(output))
}