diff --git a/README.md b/README.md index 068e0bf..be0981e 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,10 @@ example which uses the latest chi/render sub-pkg. All feedback is welcome, thank you! +## Build with [jsoniter](https://github.com/json-iterator/go) + +The `render` package use `encoding/json` as default json package but you can change to [jsoniter](https://github.com/json-iterator/go) by build from other tags. + +```sh +$ go build -tags=jsoniter . +``` diff --git a/decoder.go b/decoder.go index 849c56b..cfe8144 100644 --- a/decoder.go +++ b/decoder.go @@ -1,12 +1,13 @@ package render import ( - "encoding/json" "encoding/xml" "errors" "io" "io/ioutil" "net/http" + + "github.com/go-chi/render/json" ) // Decode is a package-level variable set to our default Decoder. We do this diff --git a/json/json.go b/json/json.go new file mode 100644 index 0000000..e5b10fd --- /dev/null +++ b/json/json.go @@ -0,0 +1,11 @@ +// +build !jsoniter + +package json + +import "encoding/json" + +var ( + Marshal = json.Marshal + NewDecoder = json.NewDecoder + NewEncoder = json.NewEncoder +) diff --git a/json/jsoniter.go b/json/jsoniter.go new file mode 100644 index 0000000..383f195 --- /dev/null +++ b/json/jsoniter.go @@ -0,0 +1,12 @@ +// +build jsoniter + +package json + +import jsoniter "github.com/json-iterator/go" + +var ( + json = jsoniter.ConfigCompatibleWithStandardLibrary + Marshal = json.Marshal + NewDecoder = json.NewDecoder + NewEncoder = json.NewEncoder +) diff --git a/responder.go b/responder.go index 4a7c52f..84ec173 100644 --- a/responder.go +++ b/responder.go @@ -3,11 +3,12 @@ package render import ( "bytes" "context" - "encoding/json" "encoding/xml" "fmt" "net/http" "reflect" + + "github.com/go-chi/render/json" ) // M is a convenience alias for quickly building a map structure that is going