Sfoglia il codice sorgente

use json.Marshaler then trim the last '\n' in reflect_marshaler

N/A
liyibo [李一博] 6 anni fa
parent
commit
976454858b
2 ha cambiato i file con 57 aggiunte e 2 eliminazioni
  1. 47 0
      api_tests/marshal_json_escape_test.go
  2. 10 2
      reflect_marshaler.go

+ 47 - 0
api_tests/marshal_json_escape_test.go

@@ -0,0 +1,47 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"testing"
+
+	jsoniter "github.com/json-iterator/go"
+	"github.com/stretchr/testify/require"
+)
+
+var marshalConfig = jsoniter.Config{
+	EscapeHTML:             false,
+	SortMapKeys:            true,
+	ValidateJsonRawMessage: true,
+}.Froze()
+
+type Container struct {
+	Bar interface{}
+}
+
+func (c *Container) MarshalJSON() ([]byte, error) {
+	return marshalConfig.Marshal(&c.Bar)
+}
+
+func TestEncodeEscape(t *testing.T) {
+	should := require.New(t)
+
+	container := &Container{
+		Bar: []string{"123<ab>", "ooo"},
+	}
+	out, err := marshalConfig.Marshal(container)
+	should.Nil(err)
+	bufout := string(out)
+
+	var stdbuf bytes.Buffer
+	stdenc := json.NewEncoder(&stdbuf)
+	stdenc.SetEscapeHTML(false)
+	err = stdenc.Encode(container)
+	should.Nil(err)
+	stdout := string(stdbuf.Bytes())
+	if stdout[len(stdout)-1:] == "\n" {
+		stdout = stdout[:len(stdout)-1]
+	}
+
+	should.Equal(stdout, bufout)
+}

+ 10 - 2
reflect_marshaler.go

@@ -3,8 +3,9 @@ package jsoniter
 import (
 	"encoding"
 	"encoding/json"
-	"github.com/modern-go/reflect2"
 	"unsafe"
+
+	"github.com/modern-go/reflect2"
 )
 
 var marshalerType = reflect2.TypeOfPtr((*json.Marshaler)(nil)).Elem()
@@ -93,10 +94,17 @@ func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
 		stream.WriteNil()
 		return
 	}
-	bytes, err := json.Marshal(obj)
+	marshaler := obj.(json.Marshaler)
+	bytes, err := marshaler.MarshalJSON()
 	if err != nil {
 		stream.Error = err
 	} else {
+		// html escape was already done by jsoniter
+		// but the extra '\n' should be trimed
+		l := len(bytes)
+		if l > 0 && bytes[l-1] == '\n' {
+			bytes = bytes[:l-1]
+		}
 		stream.Write(bytes)
 	}
 }