Skip to content

Commit

Permalink
extjson
Browse files Browse the repository at this point in the history
  • Loading branch information
8treenet committed Apr 28, 2021
1 parent 8c85f05 commit ac8ddcb
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions extjson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## 安装
```go
$ go get github.com/8treenet/extjson
$ go get github.com/8treenet/venus
```


Expand All @@ -31,7 +31,7 @@ type Style struct {

func TestLowerCamelCase(t *testing.T) {
//设置配置 NamedStyle: extjson.NamedStyleLowerCamelCase 小写驼峰风格
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{NamedStyle: extjson.NamedStyleLowerCamelCase})
extjson.SetDefaultOption(extjson.ExtOption{NamedStyle: extjson.NamedStyleLowerCamelCase})

//extjson.Marshal 序列化
styleBytes, _ := extjson.Marshal(Style{StyleText: "extjson", StyleNumber: 100, StyleBoolean: true, StyleTag: "Tag"})
Expand All @@ -56,7 +56,7 @@ import (

func TestUnderScoreCase(t *testing.T) {
//extjson.NamedStyleUnderScoreCase : 下划线风格
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{NamedStyle: extjson.NamedStyleUnderScoreCase})
extjson.SetDefaultOption(extjson.ExtOption{NamedStyle: extjson.NamedStyleUnderScoreCase})
styleBytes, _ := extjson.Marshal(Style{StyleText: "extjson", StyleNumber: 100, StyleBoolean: true, StyleTag: "Tag"})
fmt.Println(string(styleBytes))
//输出: {"style_text":"extjson","style_number":100,"style_boolean":true,"tagtagtag":"Tag"}
Expand All @@ -78,7 +78,7 @@ import (
)

func TestNull(t *testing.T) {
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{
extjson.SetDefaultOption(extjson.ExtOption{
NamedStyle: extjson.NamedStyleLowerCamelCase, //小写驼峰
SliceNotNull: true, //空数组不返回null, 返回[]
StructPtrNotNull: true, //nil结构体指针不返回null, 返回{}
Expand Down Expand Up @@ -140,7 +140,7 @@ import (
"net/http"
)
func init() {
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{NamedStyle: extjson.NamedStyleUnderScoreCase})
extjson.SetDefaultOption(extjson.ExtOption{NamedStyle: extjson.NamedStyleUnderScoreCase})
}

func handle(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion extjson/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ type decodeState struct {
savedError error
useNumber bool
disallowUnknownFields bool
exEntity *ExtJSONEntity
exEntity *ExtJSON
timeFormat string
}

Expand Down
12 changes: 6 additions & 6 deletions extjson/extjson.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package extjson

// SetDefaultOption .
func SetDefaultOption(entityOption ExtEntityOption) {
func SetDefaultOption(entityOption ExtOption) {
defEntity = &ExtJSON{option: entityOption}
}

// ExtEntityOption .
type ExtEntityOption struct {
// ExtOption .
type ExtOption struct {
NamedStyle int
SliceNotNull bool
StructPtrNotNull bool
}

func (exOption *ExtEntityOption) checkInvalid() {
func (exOption *ExtOption) checkInvalid() {
if exOption.NamedStyle == 0 {
exOption.NamedStyle = NamedStyleUpperCamelCase
}
}

// ExtJSON .
type ExtJSON struct {
option ExtEntityOption
option ExtOption
}

var defEntity *ExtJSON

func init() {
SetDefaultOption(ExtEntityOption{})
SetDefaultOption(ExtOption{})
}
6 changes: 3 additions & 3 deletions extjson/extjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Style struct {
}

func TestLowerCamelCase(t *testing.T) {
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{NamedStyle: extjson.NamedStyleLowerCamelCase})
extjson.SetDefaultOption(extjson.ExtOption{NamedStyle: extjson.NamedStyleLowerCamelCase})
styleBytes, _ := extjson.Marshal(Style{StyleText: "extjson", StyleNumber: 100, StyleBoolean: true, StyleTag: "Tag"})
t.Log(string(styleBytes))
//out: {"styleText":"extjson","styleNumber":100,"styleBoolean":true,"tagtagtag":"Tag"}
Expand All @@ -27,7 +27,7 @@ func TestLowerCamelCase(t *testing.T) {
}

func TestUnderScoreCase(t *testing.T) {
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{NamedStyle: extjson.NamedStyleUnderScoreCase})
extjson.SetDefaultOption(extjson.ExtOption{NamedStyle: extjson.NamedStyleUnderScoreCase})
styleBytes, _ := extjson.Marshal(Style{StyleText: "extjson", StyleNumber: 100, StyleBoolean: true, StyleTag: "Tag"})
t.Log(string(styleBytes))
//out: {"style_text":"extjson","style_number":100,"style_boolean":true,"tagtagtag":"Tag"}
Expand All @@ -39,7 +39,7 @@ func TestUnderScoreCase(t *testing.T) {
}

func TestNull(t *testing.T) {
extjson.SetDefaultOption(extjson.ExtJSONEntityOption{
extjson.SetDefaultOption(extjson.ExtOption{
NamedStyle: extjson.NamedStyleLowerCamelCase,
SliceNotNull: true,
StructPtrNotNull: true,
Expand Down
2 changes: 1 addition & 1 deletion extjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

func namedStyleCaseFormat(field string, exEntity *ExtJSONEntity) string {
func namedStyleCaseFormat(field string, exEntity *ExtJSON) string {
switch exEntity.option.NamedStyle {
case NamedStyleUnderScoreCase:
return underScoreCase(field)
Expand Down
2 changes: 1 addition & 1 deletion extjson/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ type Encoder struct {
indentBuf *bytes.Buffer
indentPrefix string
indentValue string
ex *ExtJSONEntity
ex *ExtJSON
}

// NewEncoder returns a new encoder that writes to w.
Expand Down

0 comments on commit ac8ddcb

Please sign in to comment.