Skip to content

Commit

Permalink
Merge pull request #3 from go-baa/jsoniter
Browse files Browse the repository at this point in the history
add jsoniter
  • Loading branch information
safeie authored Feb 7, 2018
2 parents 1a6dc3c + 7c121c7 commit 14ab7a6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func main() {
}
```

Build:

Baa use encoding/json as default json package but you can change to [jsoniter](https://github.com/json-iterator/go) by build from other tags

```
go build -tags=jsoniter .
```

Run:

```
Expand Down
8 changes: 8 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func main() {
}
```

编译:

Baa use encoding/json as default json package but you can change to [jsoniter](https://github.com/json-iterator/go) by build from other tags

```
go build -tags=jsoniter .
```

运行:

```
Expand Down
13 changes: 6 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package baa

import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand Down Expand Up @@ -288,7 +287,7 @@ func (c *Context) QueryJSON(v interface{}) error {
if len(content) == 0 {
return ErrJSONPayloadEmpty
}
return json.Unmarshal(content, v)
return Unmarshal(content, v)
}

// QueryXML decode xml from http.Request.Body
Expand Down Expand Up @@ -444,9 +443,9 @@ func (c *Context) JSON(code int, v interface{}) {
var re []byte
var err error
if c.baa.debug {
re, err = json.MarshalIndent(v, "", " ")
re, err = MarshalIndent(v, "", " ")
} else {
re, err = json.Marshal(v)
re, err = Marshal(v)
}
if err != nil {
c.Error(err)
Expand All @@ -463,9 +462,9 @@ func (c *Context) JSONString(v interface{}) (string, error) {
var re []byte
var err error
if c.baa.debug {
re, err = json.MarshalIndent(v, "", " ")
re, err = MarshalIndent(v, "", " ")
} else {
re, err = json.Marshal(v)
re, err = Marshal(v)
}
if err != nil {
return "", err
Expand All @@ -475,7 +474,7 @@ func (c *Context) JSONString(v interface{}) (string, error) {

// JSONP write data by jsonp format
func (c *Context) JSONP(code int, callback string, v interface{}) {
re, err := json.Marshal(v)
re, err := Marshal(v)
if err != nil {
c.Error(err)
return
Expand Down
7 changes: 3 additions & 4 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package baa

import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
Expand Down Expand Up @@ -226,7 +225,7 @@ func TestContextQuery1(t *testing.T) {
c.QueryJSON(&dataFromBody)
So(dataFromBody["test"], ShouldEqual, dataSource["test"])
})
body, _ := json.Marshal(dataSource)
body, _ := Marshal(dataSource)
req, _ := http.NewRequest("POST", "/context/json", bytes.NewReader(body))
req.Header.Set("Content-Type", ApplicationJSON)
w := httptest.NewRecorder()
Expand All @@ -241,7 +240,7 @@ func TestContextQuery1(t *testing.T) {
c.QueryJSON(dataFromBody)
So(dataFromBody, ShouldBeNil)
})
body, _ := json.Marshal(dataSource)
body, _ := Marshal(dataSource)
req, _ := http.NewRequest("POST", "/context/json2", bytes.NewReader(body))
req.Header.Set("Content-Type", ApplicationJSON)
w := httptest.NewRecorder()
Expand All @@ -258,7 +257,7 @@ func TestContextQuery1(t *testing.T) {
c.QueryJSON(&dataFromBody)
So(dataFromBody.Test, ShouldEqual, dataSource["test"])
})
body, _ := json.Marshal(dataSource)
body, _ := Marshal(dataSource)
req, _ := http.NewRequest("POST", "/context/json3", bytes.NewReader(body))
req.Header.Set("Content-Type", ApplicationJSON)
w := httptest.NewRecorder()
Expand Down
11 changes: 11 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build !jsoniter

package baa

import "encoding/json"

var (
Marshal = json.Marshal
Unmarshal = json.Unmarshal
MarshalIndent = json.MarshalIndent
)
12 changes: 12 additions & 0 deletions jsoniter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build jsoniter

package baa

import "github.com/json-iterator/go"

var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
Marshal = json.Marshal
Unmarshal = json.Unmarshal
MarshalIndent = json.MarshalIndent
)

0 comments on commit 14ab7a6

Please sign in to comment.