diff --git a/bind.go b/bind.go index 8f54ae41b..722fe427c 100644 --- a/bind.go +++ b/bind.go @@ -1,7 +1,7 @@ package echo import ( - "encoding/json" + "github.com/labstack/echo/json" "encoding/xml" "errors" "fmt" diff --git a/bind_test.go b/bind_test.go index 72b0ed7d6..ab49e4d21 100644 --- a/bind_test.go +++ b/bind_test.go @@ -2,7 +2,7 @@ package echo import ( "bytes" - "encoding/json" + "github.com/labstack/echo/json" "encoding/xml" "errors" "io" @@ -255,9 +255,10 @@ func TestBindUnmarshalTypeError(t *testing.T) { err := c.Bind(u) - he := &HTTPError{Code: http.StatusBadRequest, Message: "Unmarshal type error: expected=int, got=string, field=id, offset=14", Internal: err.(*HTTPError).Internal} - - assert.Equal(t, he, err) + if json.PKG == json.STDJSON { + he := &HTTPError{Code: http.StatusBadRequest, Message: "Unmarshal type error: expected=int, got=string, field=id, offset=14", Internal: err.(*HTTPError).Internal} + assert.Equal(t, he, err) + } } func TestBindSetWithProperType(t *testing.T) { diff --git a/context.go b/context.go index 9a9d133ed..05e744f4b 100644 --- a/context.go +++ b/context.go @@ -2,7 +2,7 @@ package echo import ( "bytes" - "encoding/json" + "github.com/labstack/echo/json" "encoding/xml" "fmt" "io" diff --git a/go.mod b/go.mod index b2b9f182d..29359b650 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,12 @@ module github.com/labstack/echo require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/json-iterator/go v1.1.5 // indirect github.com/labstack/gommon v0.2.7 github.com/mattn/go-colorable v0.0.9 // indirect github.com/mattn/go-isatty v0.0.3 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.1 github.com/valyala/bytebufferpool v0.0.0-20160817181652-e746df99fe4a // indirect diff --git a/go.sum b/go.sum index 7d5ffe073..5439d46e5 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,18 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= +github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/labstack/gommon v0.2.7 h1:2qOPq/twXDrQ6ooBGrn3mrmVOC+biLlatwgIu8lbzRM= github.com/labstack/gommon v0.2.7/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U= diff --git a/json/common.go b/json/common.go new file mode 100644 index 000000000..37a4e9291 --- /dev/null +++ b/json/common.go @@ -0,0 +1,14 @@ +package json + +import ( + "encoding/json" +) + +type UnmarshalTypeError = json.UnmarshalTypeError +type SyntaxError = json.SyntaxError +type RawMessage = json.RawMessage + +const ( + STDJSON = iota + JSONITER +) diff --git a/json/json.go b/json/json.go new file mode 100644 index 000000000..0281ba96c --- /dev/null +++ b/json/json.go @@ -0,0 +1,15 @@ +// +build !jsoniter + +package json + +import ( + j "encoding/json" +) + +var ( + PKG = STDJSON + Marshal = j.Marshal + MarshalIndent = j.MarshalIndent + Unmarshal = j.Unmarshal + NewDecoder = j.NewDecoder +) diff --git a/json/jsoniter.go b/json/jsoniter.go new file mode 100644 index 000000000..565d3d519 --- /dev/null +++ b/json/jsoniter.go @@ -0,0 +1,16 @@ +// +build jsoniter + +package json + +import ( + "github.com/json-iterator/go" +) + +var ( + PKG = JSONITER + jnt = jsoniter.ConfigCompatibleWithStandardLibrary + Marshal = jnt.Marshal + MarshalIndent = jnt.MarshalIndent + Unmarshal = jnt.Unmarshal + NewDecoder = jnt.NewDecoder +) diff --git a/middleware/logger_test.go b/middleware/logger_test.go index ea43befda..bdc30685b 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -2,7 +2,7 @@ package middleware import ( "bytes" - "encoding/json" + "github.com/labstack/echo/json" "errors" "net/http" "net/http/httptest"