Skip to content

support jsoniter #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package echo

import (
"encoding/json"
"github.com/labstack/echo/json"
"encoding/xml"
"errors"
"fmt"
Expand Down
9 changes: 5 additions & 4 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package echo

import (
"bytes"
"encoding/json"
"github.com/labstack/echo/json"
"encoding/xml"
"errors"
"io"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package echo

import (
"bytes"
"encoding/json"
"github.com/labstack/echo/json"
"encoding/xml"
"fmt"
"io"
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 14 additions & 0 deletions json/common.go
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 15 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
@@ -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
)
16 changes: 16 additions & 0 deletions json/jsoniter.go
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middleware

import (
"bytes"
"encoding/json"
"github.com/labstack/echo/json"
"errors"
"net/http"
"net/http/httptest"
Expand Down