瀏覽代碼

fix json sort the map keys

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Bo-Yi Wu 8 年之前
父節點
當前提交
e23842ecab
共有 5 個文件被更改,包括 12 次插入7 次删除
  1. 3 1
      binding/json.go
  2. 1 1
      context_test.go
  3. 3 1
      errors.go
  4. 2 3
      errors_test.go
  5. 3 1
      render/json.go

+ 3 - 1
binding/json.go

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

+ 1 - 1
context_test.go

@@ -582,7 +582,7 @@ 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, "{\n    \"foo\":\"bar\",\n    \"bar\":\"foo\",\n    \"nested\":{\n        \"foo\":\"bar\"\n    }\n}", w.Body.String())
+	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"))
 	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
 }
 }
 
 

+ 3 - 1
errors.go

@@ -9,9 +9,11 @@ import (
 	"fmt"
 	"fmt"
 	"reflect"
 	"reflect"
 
 
-	json "github.com/json-iterator/go"
+	"github.com/json-iterator/go"
 )
 )
 
 
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
 type ErrorType uint64
 type ErrorType uint64
 
 
 const (
 const (

+ 2 - 3
errors_test.go

@@ -8,7 +8,6 @@ import (
 	"errors"
 	"errors"
 	"testing"
 	"testing"
 
 
-	json "github.com/json-iterator/go"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/assert"
 )
 )
 
 
@@ -32,7 +31,7 @@ func TestError(t *testing.T) {
 	})
 	})
 
 
 	jsonBytes, _ := json.Marshal(err)
 	jsonBytes, _ := json.Marshal(err)
-	assert.Equal(t, "{\"meta\":\"some data\",\"error\":\"test error\"}", string(jsonBytes))
+	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, "[{\"error\":\"first\"},{\"meta\":\"some data\",\"error\":\"second\"},{\"status\":\"400\",\"error\":\"third\"}]", string(jsonBytes))
+	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},
 	}
 	}

+ 3 - 1
render/json.go

@@ -8,9 +8,11 @@ import (
 	"bytes"
 	"bytes"
 	"net/http"
 	"net/http"
 
 
-	json "github.com/json-iterator/go"
+	"github.com/json-iterator/go"
 )
 )
 
 
+var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
 type JSON struct {
 type JSON struct {
 	Data interface{}
 	Data interface{}
 }
 }