mirror of https://github.com/gin-gonic/gin.git
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
|
package binding
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
type jsonBinding struct{}
|
type jsonBinding struct{}
|
||||||
|
|
||||||
func (jsonBinding) Name() string {
|
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"}})
|
c.IndentedJSON(201, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
|
||||||
|
|
||||||
assert.Equal(t, w.Code, 201)
|
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, "{\n \"bar\":\"foo\",\n \"foo\":\"bar\",\n \"nested\":{\n \"foo\":\"bar\"\n }\n}", 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 no Custom JSON is rendered if code is 204
|
// 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, 204, w.Code)
|
||||||
assert.Equal(t, "", w.Body.String())
|
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
|
// Tests that the response is serialized as Secure JSON
|
||||||
|
|
|
@ -6,11 +6,14 @@ package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
type ErrorType uint64
|
type ErrorType uint64
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -32,7 +31,7 @@ func TestError(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
jsonBytes, _ := json.Marshal(err)
|
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{
|
err.SetMeta(H{
|
||||||
"status": "200",
|
"status": "200",
|
||||||
|
@ -91,7 +90,7 @@ Error #03: third
|
||||||
H{"error": "third", "status": "400"},
|
H{"error": "third", "status": "400"},
|
||||||
})
|
})
|
||||||
jsonBytes, _ := json.Marshal(errs)
|
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{
|
errs = errorMsgs{
|
||||||
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,13 @@ package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
type JSON struct {
|
type JSON struct {
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,12 @@
|
||||||
"revision": "5a0f697c9ed9d68fef0116532c6e05cfeae00e55",
|
"revision": "5a0f697c9ed9d68fef0116532c6e05cfeae00e55",
|
||||||
"revisionTime": "2017-06-01T23:02:30Z"
|
"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=",
|
"checksumSHA1": "9if9IBLsxkarJ804NPWAzgskIAk=",
|
||||||
"path": "github.com/manucorporat/stats",
|
"path": "github.com/manucorporat/stats",
|
||||||
|
|
Loading…
Reference in New Issue