Skip to content

Commit

Permalink
Merge pull request gin-gonic#990 from appleboy/json
Browse files Browse the repository at this point in the history
feat: change json lib to jsoniter
  • Loading branch information
javierprovecho authored Jul 8, 2017
2 parents c4249f9 + e23842e commit ad08765
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
5 changes: 4 additions & 1 deletion binding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
5 changes: 2 additions & 3 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package gin

import (
"encoding/json"
"errors"
"testing"

Expand All @@ -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",
Expand Down Expand Up @@ -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},
}
Expand Down
5 changes: 4 additions & 1 deletion render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down
6 changes: 6 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ad08765

Please sign in to comment.