Skip to content

Commit 14ab7a6

Browse files
author
safeie
authored
Merge pull request #3 from go-baa/jsoniter
add jsoniter
2 parents 1a6dc3c + 7c121c7 commit 14ab7a6

6 files changed

Lines changed: 48 additions & 11 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ func main() {
3737
}
3838
```
3939

40+
Build:
41+
42+
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
43+
44+
```
45+
go build -tags=jsoniter .
46+
```
47+
4048
Run:
4149

4250
```

README_zh-CN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ func main() {
3737
}
3838
```
3939

40+
编译:
41+
42+
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
43+
44+
```
45+
go build -tags=jsoniter .
46+
```
47+
4048
运行:
4149

4250
```

context.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package baa
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"encoding/xml"
76
"errors"
87
"fmt"
@@ -288,7 +287,7 @@ func (c *Context) QueryJSON(v interface{}) error {
288287
if len(content) == 0 {
289288
return ErrJSONPayloadEmpty
290289
}
291-
return json.Unmarshal(content, v)
290+
return Unmarshal(content, v)
292291
}
293292

294293
// QueryXML decode xml from http.Request.Body
@@ -444,9 +443,9 @@ func (c *Context) JSON(code int, v interface{}) {
444443
var re []byte
445444
var err error
446445
if c.baa.debug {
447-
re, err = json.MarshalIndent(v, "", " ")
446+
re, err = MarshalIndent(v, "", " ")
448447
} else {
449-
re, err = json.Marshal(v)
448+
re, err = Marshal(v)
450449
}
451450
if err != nil {
452451
c.Error(err)
@@ -463,9 +462,9 @@ func (c *Context) JSONString(v interface{}) (string, error) {
463462
var re []byte
464463
var err error
465464
if c.baa.debug {
466-
re, err = json.MarshalIndent(v, "", " ")
465+
re, err = MarshalIndent(v, "", " ")
467466
} else {
468-
re, err = json.Marshal(v)
467+
re, err = Marshal(v)
469468
}
470469
if err != nil {
471470
return "", err
@@ -475,7 +474,7 @@ func (c *Context) JSONString(v interface{}) (string, error) {
475474

476475
// JSONP write data by jsonp format
477476
func (c *Context) JSONP(code int, callback string, v interface{}) {
478-
re, err := json.Marshal(v)
477+
re, err := Marshal(v)
479478
if err != nil {
480479
c.Error(err)
481480
return

context_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package baa
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"encoding/xml"
76
"fmt"
87
"io"
@@ -226,7 +225,7 @@ func TestContextQuery1(t *testing.T) {
226225
c.QueryJSON(&dataFromBody)
227226
So(dataFromBody["test"], ShouldEqual, dataSource["test"])
228227
})
229-
body, _ := json.Marshal(dataSource)
228+
body, _ := Marshal(dataSource)
230229
req, _ := http.NewRequest("POST", "/context/json", bytes.NewReader(body))
231230
req.Header.Set("Content-Type", ApplicationJSON)
232231
w := httptest.NewRecorder()
@@ -241,7 +240,7 @@ func TestContextQuery1(t *testing.T) {
241240
c.QueryJSON(dataFromBody)
242241
So(dataFromBody, ShouldBeNil)
243242
})
244-
body, _ := json.Marshal(dataSource)
243+
body, _ := Marshal(dataSource)
245244
req, _ := http.NewRequest("POST", "/context/json2", bytes.NewReader(body))
246245
req.Header.Set("Content-Type", ApplicationJSON)
247246
w := httptest.NewRecorder()
@@ -258,7 +257,7 @@ func TestContextQuery1(t *testing.T) {
258257
c.QueryJSON(&dataFromBody)
259258
So(dataFromBody.Test, ShouldEqual, dataSource["test"])
260259
})
261-
body, _ := json.Marshal(dataSource)
260+
body, _ := Marshal(dataSource)
262261
req, _ := http.NewRequest("POST", "/context/json3", bytes.NewReader(body))
263262
req.Header.Set("Content-Type", ApplicationJSON)
264263
w := httptest.NewRecorder()

json.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build !jsoniter
2+
3+
package baa
4+
5+
import "encoding/json"
6+
7+
var (
8+
Marshal = json.Marshal
9+
Unmarshal = json.Unmarshal
10+
MarshalIndent = json.MarshalIndent
11+
)

jsoniter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build jsoniter
2+
3+
package baa
4+
5+
import "github.com/json-iterator/go"
6+
7+
var (
8+
json = jsoniter.ConfigCompatibleWithStandardLibrary
9+
Marshal = json.Marshal
10+
Unmarshal = json.Unmarshal
11+
MarshalIndent = json.MarshalIndent
12+
)

0 commit comments

Comments
 (0)