| 1 |
package extra |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/json-iterator/go" |
| 5 |
"github.com/stretchr/testify/require" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func Test_lower_case_with_underscores(t *testing.T) { |
| 10 |
should := require.New(t) |
| 11 |
should.Equal("hello_world", LowerCaseWithUnderscores("helloWorld")) |
| 12 |
should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld")) |
| 13 |
SetNamingStrategy(LowerCaseWithUnderscores) |
| 14 |
output, err := jsoniter.Marshal(struct { |
| 15 |
UserName string |
| 16 |
FirstLanguage string |
| 17 |
}{ |
| 18 |
UserName: "taowen", |
| 19 |
FirstLanguage: "Chinese", |
| 20 |
}) |
| 21 |
should.Nil(err) |
| 22 |
should.Equal(`{"user_name":"taowen","first_language":"Chinese"}`, string(output)) |
| 23 |
} |
| 24 |
|
| 25 |
func Test_set_naming_strategy_with_overrides(t *testing.T) { |
| 26 |
should := require.New(t) |
| 27 |
SetNamingStrategy(LowerCaseWithUnderscores) |
| 28 |
output, err := jsoniter.Marshal(struct { |
| 29 |
UserName string `json:"UserName"` |
| 30 |
FirstLanguage string |
| 31 |
}{ |
| 32 |
UserName: "taowen", |
| 33 |
FirstLanguage: "Chinese", |
| 34 |
}) |
| 35 |
should.Nil(err) |
| 36 |
should.Equal(`{"UserName":"taowen","first_language":"Chinese"}`, string(output)) |
| 37 |
} |
| 38 |
|
| 39 |
func Test_set_naming_strategy_with_omitempty(t *testing.T) { |
| 40 |
should := require.New(t) |
| 41 |
SetNamingStrategy(LowerCaseWithUnderscores) |
| 42 |
output, err := jsoniter.Marshal(struct { |
| 43 |
UserName string |
| 44 |
FirstLanguage string `json:",omitempty"` |
| 45 |
}{ |
| 46 |
UserName: "taowen", |
| 47 |
}) |
| 48 |
should.Nil(err) |
| 49 |
should.Equal(`{"user_name":"taowen"}`, string(output)) |
| 50 |
} |
| 51 |
|
| 52 |
func Test_set_naming_strategy_with_private_field(t *testing.T) { |
| 53 |
should := require.New(t) |
| 54 |
SetNamingStrategy(LowerCaseWithUnderscores) |
| 55 |
output, err := jsoniter.Marshal(struct { |
| 56 |
UserName string |
| 57 |
userId int |
| 58 |
_UserAge int |
| 59 |
}{ |
| 60 |
UserName: "allen", |
| 61 |
userId: 100, |
| 62 |
_UserAge: 30, |
| 63 |
}) |
| 64 |
should.Nil(err) |
| 65 |
should.Equal(`{"user_name":"allen"}`, string(output)) |
| 66 |
} |