瀏覽代碼

Merge pull request #990 from appleboy/json

feat: change json lib to jsoniter
Javier Provecho Fernandez 8 年之前
父節點
當前提交
ad087650e9
共有 6 個文件被更改,包括 23 次插入9 次删除
  1. 4 1
      binding/json.go
  2. 3 3
      context_test.go
  3. 4 1
      errors.go
  4. 2 3
      errors_test.go
  5. 4 1
      render/json.go
  6. 6 0
      vendor/vendor.json

+ 4 - 1
binding/json.go

@@ -5,10 +5,13 @@
 package binding
 
 import (
-	"encoding/json"
 	"net/http"
+
+	"github.com/json-iterator/go"
 )
 
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
 type jsonBinding struct{}
 
 func (jsonBinding) Name() string {

+ 3 - 3
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    \"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"))
 }
 
 // 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

+ 4 - 1
errors.go

@@ -6,11 +6,14 @@ package gin
 
 import (
 	"bytes"
-	"encoding/json"
 	"fmt"
 	"reflect"
+
+	"github.com/json-iterator/go"
 )
 
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
 type ErrorType uint64
 
 const (

+ 2 - 3
errors_test.go

@@ -5,7 +5,6 @@
 package gin
 
 import (
-	"encoding/json"
 	"errors"
 	"testing"
 
@@ -32,7 +31,7 @@ func TestError(t *testing.T) {
 	})
 
 	jsonBytes, _ := json.Marshal(err)
-	assert.Equal(t, string(jsonBytes), "{\"error\":\"test error\",\"meta\":\"some data\"}")
+	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, string(jsonBytes), "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]")
+	assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
 	errs = errorMsgs{
 		{Err: errors.New("first"), Type: ErrorTypePrivate},
 	}

+ 4 - 1
render/json.go

@@ -6,10 +6,13 @@ package render
 
 import (
 	"bytes"
-	"encoding/json"
 	"net/http"
+
+	"github.com/json-iterator/go"
 )
 
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
 type JSON struct {
 	Data interface{}
 }

+ 6 - 0
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",