From 4ef1d800c39cab4ca2f0e6adb67c9c0d404dd768 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 17 Jul 2017 10:03:45 +0800 Subject: [PATCH 1/5] refactor(json): Restore gin support for app engine Create new folder to support multiple json package. restore gin support for app engine (disable jsonite through tags) use jsoniter $ go build -tags=jsoniter . use default json $ go build . Signed-off-by: Bo-Yi Wu --- binding/json.go | 3 +-- errors.go | 4 +--- errors_test.go | 1 + json/json_base.go | 17 +++++++++++++++++ json/json_iter.go | 18 ++++++++++++++++++ render/json.go | 4 +--- 6 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 json/json_base.go create mode 100644 json/json_iter.go diff --git a/binding/json.go b/binding/json.go index 215fa9bfb4..f600a5f477 100644 --- a/binding/json.go +++ b/binding/json.go @@ -7,11 +7,10 @@ package binding import ( "net/http" - "github.com/json-iterator/go" + "github.com/gin-gonic/gin/json" ) var ( - json = jsoniter.ConfigCompatibleWithStandardLibrary EnableDecoderUseNumber = false ) diff --git a/errors.go b/errors.go index 26d4e474c2..1976110643 100644 --- a/errors.go +++ b/errors.go @@ -9,11 +9,9 @@ import ( "fmt" "reflect" - "github.com/json-iterator/go" + "github.com/gin-gonic/gin/json" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - type ErrorType uint64 const ( diff --git a/errors_test.go b/errors_test.go index a0d955049c..5e596aff9e 100644 --- a/errors_test.go +++ b/errors_test.go @@ -8,6 +8,7 @@ import ( "errors" "testing" + "github.com/gin-gonic/gin/json" "github.com/stretchr/testify/assert" ) diff --git a/json/json_base.go b/json/json_base.go new file mode 100644 index 0000000000..57172ee68a --- /dev/null +++ b/json/json_base.go @@ -0,0 +1,17 @@ +// Copyright 2017 Bo-Yi Wu. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +//+build !jsoniter + +package json + +import ( + "encoding/json" +) + +var ( + Marshal = json.Marshal + MarshalIndent = json.MarshalIndent + NewDecoder = json.NewDecoder +) diff --git a/json/json_iter.go b/json/json_iter.go new file mode 100644 index 0000000000..8e01f116b1 --- /dev/null +++ b/json/json_iter.go @@ -0,0 +1,18 @@ +// Copyright 2017 Bo-Yi Wu. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +//+build jsoniter + +package json + +import ( + "github.com/json-iterator/go" +) + +var ( + json = jsoniter.ConfigCompatibleWithStandardLibrary + Marshal = json.Marshal + MarshalIndent = json.MarshalIndent + NewDecoder = json.NewDecoder +) diff --git a/render/json.go b/render/json.go index 17e6ac8625..eb2548e2ad 100644 --- a/render/json.go +++ b/render/json.go @@ -8,11 +8,9 @@ import ( "bytes" "net/http" - "github.com/json-iterator/go" + "github.com/gin-gonic/gin/json" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - type JSON struct { Data interface{} } From afb36dc600a1e6da4a2e7f261985a14789a82040 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 17 Jul 2017 10:17:06 +0800 Subject: [PATCH 2/5] rename json file. Signed-off-by: Bo-Yi Wu --- json/{json_base.go => json.go} | 0 json/{json_iter.go => jsoniter.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename json/{json_base.go => json.go} (100%) rename json/{json_iter.go => jsoniter.go} (100%) diff --git a/json/json_base.go b/json/json.go similarity index 100% rename from json/json_base.go rename to json/json.go diff --git a/json/json_iter.go b/json/jsoniter.go similarity index 100% rename from json/json_iter.go rename to json/jsoniter.go From f057548d7fb396bded448d8901eb7747ef36e8e8 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 18 Jul 2017 08:51:18 +0800 Subject: [PATCH 3/5] docs(json): add build tags document. --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e3df99f65..07ef33fc28 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,6 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648 - [x] Battle tested - [x] API frozen, new releases will not break your code. - ## Start using it 1. Download and install it: @@ -141,6 +140,14 @@ $ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/mai $ go run main.go ``` +## Build with [jsoniter](https://github.com/json-iterator/go) + +Gin 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 . +``` + ## API Examples ### Using GET, POST, PUT, PATCH, DELETE and OPTIONS From 9211ab7377bec4a3c342ae3726437a42380706e9 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 18 Jul 2017 08:52:50 +0800 Subject: [PATCH 4/5] fix(docs): markdown format. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07ef33fc28..76ab3b0118 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ $ go run main.go Gin 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 . ``` From 3e623c1d45e2b9124fb3e2cbc0ed921d1902eaa0 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 18 Jul 2017 22:23:26 +0800 Subject: [PATCH 5/5] fix(json): missing space. --- json/json.go | 2 +- json/jsoniter.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/json/json.go b/json/json.go index 57172ee68a..d2d0f8b37a 100644 --- a/json/json.go +++ b/json/json.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -//+build !jsoniter +// +build !jsoniter package json diff --git a/json/jsoniter.go b/json/jsoniter.go index 8e01f116b1..65deee5999 100644 --- a/json/jsoniter.go +++ b/json/jsoniter.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -//+build jsoniter +// +build jsoniter package json