From 12508320c2834204677e29be58fec8103facdaf8 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 8 Jul 2017 16:49:09 +0800 Subject: [PATCH 1/4] feat: change json lib to jsoniter A high-performance 100% compatible drop-in replacement of "encoding/json" https://github.com/json-iterator/go Signed-off-by: Bo-Yi Wu --- binding/json.go | 3 ++- errors.go | 3 ++- errors_test.go | 2 +- render/json.go | 3 ++- vendor/vendor.json | 6 ++++++ 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/binding/json.go b/binding/json.go index 486b973..aa7039d 100644 --- a/binding/json.go +++ b/binding/json.go @@ -5,8 +5,9 @@ package binding import ( - "encoding/json" "net/http" + + json "github.com/json-iterator/go" ) type jsonBinding struct{} diff --git a/errors.go b/errors.go index f38d3ff..19b2cfb 100644 --- a/errors.go +++ b/errors.go @@ -6,9 +6,10 @@ package gin import ( "bytes" - "encoding/json" "fmt" "reflect" + + json "github.com/json-iterator/go" ) type ErrorType uint64 diff --git a/errors_test.go b/errors_test.go index 1aa0cdd..46ae536 100644 --- a/errors_test.go +++ b/errors_test.go @@ -5,10 +5,10 @@ package gin import ( - "encoding/json" "errors" "testing" + json "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) diff --git a/render/json.go b/render/json.go index 8b64f53..3896740 100644 --- a/render/json.go +++ b/render/json.go @@ -6,8 +6,9 @@ package render import ( "bytes" - "encoding/json" "net/http" + + json "github.com/json-iterator/go" ) type JSON struct { diff --git a/vendor/vendor.json b/vendor/vendor.json index e520540..547bb2e 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -33,6 +33,12 @@ "revision": "5a0f697c9ed9d68fef0116532c6e05cfeae00e55", "revisionTime": "2017-06-01T23:02:30Z" }, + { + "checksumSHA1": "gWQ2ncPI6qpTwS3e6/ShPwUP1uo=", + "path": "github.com/json-iterator/go", + "revision": "b1afefe0580e6e818dd50da9593f477c80ccd67d", + "revisionTime": "2017-07-07T13:43:33Z" + }, { "checksumSHA1": "9if9IBLsxkarJ804NPWAzgskIAk=", "path": "github.com/manucorporat/stats", From 08338eff821be0a8805aab51ac35eac856e6a3ed Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 8 Jul 2017 17:03:14 +0800 Subject: [PATCH 2/4] fix testing Signed-off-by: Bo-Yi Wu --- context_test.go | 6 +++--- errors_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/context_test.go b/context_test.go index 96e9f08..6c4af42 100644 --- a/context_test.go +++ b/context_test.go @@ -582,8 +582,8 @@ func TestContextRenderIndentedJSON(t *testing.T) { c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}}) assert.Equal(t, w.Code, 201) - assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}") - assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") + assert.Equal(t, "{\n \"foo\":\"bar\",\n \"bar\":\"foo\",\n \"nested\":{\n \"foo\":\"bar\"\n }\n}", w.Body.String()) + assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) } // Tests that no Custom JSON is rendered if code is 204 @@ -595,7 +595,7 @@ func TestContextRenderNoContentIndentedJSON(t *testing.T) { assert.Equal(t, 204, w.Code) assert.Equal(t, "", w.Body.String()) - assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") + assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) } // Tests that the response is serialized as Secure JSON diff --git a/errors_test.go b/errors_test.go index 46ae536..85782c0 100644 --- a/errors_test.go +++ b/errors_test.go @@ -91,7 +91,7 @@ Error #03: third H{"error": "third", "status": "400"}, }) jsonBytes, _ := json.Marshal(errs) - assert.Equal(t, string(jsonBytes), "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]") + assert.Equal(t, "[{\"error\":\"first\"},{\"meta\":\"some data\",\"error\":\"second\"},{\"status\":\"400\",\"error\":\"third\"}]", string(jsonBytes)) errs = errorMsgs{ {Err: errors.New("first"), Type: ErrorTypePrivate}, } From cb524fc94e322efd6b1bf82688af5109cffd06c7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 8 Jul 2017 17:11:28 +0800 Subject: [PATCH 3/4] fix testing Signed-off-by: Bo-Yi Wu --- errors_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/errors_test.go b/errors_test.go index 85782c0..778ecf0 100644 --- a/errors_test.go +++ b/errors_test.go @@ -32,7 +32,7 @@ func TestError(t *testing.T) { }) jsonBytes, _ := json.Marshal(err) - assert.Equal(t, string(jsonBytes), "{\"error\":\"test error\",\"meta\":\"some data\"}") + assert.Equal(t, "{\"meta\":\"some data\",\"error\":\"test error\"}", string(jsonBytes)) err.SetMeta(H{ "status": "200", From e23842ecab161390b6b537c1c906d7e713f07db0 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 8 Jul 2017 18:19:09 +0800 Subject: [PATCH 4/4] fix json sort the map keys Signed-off-by: Bo-Yi Wu --- binding/json.go | 4 +++- context_test.go | 2 +- errors.go | 4 +++- errors_test.go | 5 ++--- render/json.go | 4 +++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/binding/json.go b/binding/json.go index aa7039d..20aae8c 100644 --- a/binding/json.go +++ b/binding/json.go @@ -7,9 +7,11 @@ package binding import ( "net/http" - json "github.com/json-iterator/go" + "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type jsonBinding struct{} func (jsonBinding) Name() string { diff --git a/context_test.go b/context_test.go index 6c4af42..f57fa6f 100644 --- a/context_test.go +++ b/context_test.go @@ -582,7 +582,7 @@ func TestContextRenderIndentedJSON(t *testing.T) { c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}}) assert.Equal(t, w.Code, 201) - assert.Equal(t, "{\n \"foo\":\"bar\",\n \"bar\":\"foo\",\n \"nested\":{\n \"foo\":\"bar\"\n }\n}", w.Body.String()) + assert.Equal(t, "{\n \"bar\":\"foo\",\n \"foo\":\"bar\",\n \"nested\":{\n \"foo\":\"bar\"\n }\n}", w.Body.String()) assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) } diff --git a/errors.go b/errors.go index 19b2cfb..26d4e47 100644 --- a/errors.go +++ b/errors.go @@ -9,9 +9,11 @@ import ( "fmt" "reflect" - json "github.com/json-iterator/go" + "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type ErrorType uint64 const ( diff --git a/errors_test.go b/errors_test.go index 778ecf0..a0d9550 100644 --- a/errors_test.go +++ b/errors_test.go @@ -8,7 +8,6 @@ import ( "errors" "testing" - json "github.com/json-iterator/go" "github.com/stretchr/testify/assert" ) @@ -32,7 +31,7 @@ func TestError(t *testing.T) { }) jsonBytes, _ := json.Marshal(err) - assert.Equal(t, "{\"meta\":\"some data\",\"error\":\"test error\"}", string(jsonBytes)) + assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes)) err.SetMeta(H{ "status": "200", @@ -91,7 +90,7 @@ Error #03: third H{"error": "third", "status": "400"}, }) jsonBytes, _ := json.Marshal(errs) - assert.Equal(t, "[{\"error\":\"first\"},{\"meta\":\"some data\",\"error\":\"second\"},{\"status\":\"400\",\"error\":\"third\"}]", string(jsonBytes)) + assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes)) errs = errorMsgs{ {Err: errors.New("first"), Type: ErrorTypePrivate}, } diff --git a/render/json.go b/render/json.go index 3896740..17e6ac8 100644 --- a/render/json.go +++ b/render/json.go @@ -8,9 +8,11 @@ import ( "bytes" "net/http" - json "github.com/json-iterator/go" + "github.com/json-iterator/go" ) +var json = jsoniter.ConfigCompatibleWithStandardLibrary + type JSON struct { Data interface{} }