mirror of
				https://github.com/gin-gonic/gin.git
				synced 2025-11-04 09:12:12 +08:00 
			
		
		
		
	Merge pull request #990 from appleboy/json
feat: change json lib to jsoniter
This commit is contained in:
		
						commit
						ad087650e9
					
				@ -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 {
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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 (
 | 
			
		||||
 | 
			
		||||
@ -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},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -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
									
								
								vendor/vendor.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								vendor/vendor.json
									
									
									
									
										vendored
									
									
								
							@ -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",
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user