diff --git a/README.md b/README.md index 848b126..795bc81 100644 --- a/README.md +++ b/README.md @@ -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: ``` diff --git a/README_zh-CN.md b/README_zh-CN.md index 1cfecc7..f2db185 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -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 . +``` + 运行: ``` diff --git a/context.go b/context.go index fc9004a..34fab42 100644 --- a/context.go +++ b/context.go @@ -2,7 +2,6 @@ package baa import ( "bytes" - "encoding/json" "encoding/xml" "errors" "fmt" @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/context_test.go b/context_test.go index 50b83f5..b74a6eb 100644 --- a/context_test.go +++ b/context_test.go @@ -2,7 +2,6 @@ package baa import ( "bytes" - "encoding/json" "encoding/xml" "fmt" "io" @@ -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() @@ -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() @@ -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() diff --git a/json.go b/json.go new file mode 100644 index 0000000..a8b4272 --- /dev/null +++ b/json.go @@ -0,0 +1,11 @@ +// +build !jsoniter + +package baa + +import "encoding/json" + +var ( + Marshal = json.Marshal + Unmarshal = json.Unmarshal + MarshalIndent = json.MarshalIndent +) diff --git a/jsoniter.go b/jsoniter.go new file mode 100644 index 0000000..e5682c4 --- /dev/null +++ b/jsoniter.go @@ -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 +)