...

Source file src/github.com/json-iterator/go/api_tests/marshal_json_escape_test.go

Documentation: github.com/json-iterator/go/api_tests

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	jsoniter "github.com/json-iterator/go"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  var marshalConfig = jsoniter.Config{
    13  	EscapeHTML:             false,
    14  	SortMapKeys:            true,
    15  	ValidateJsonRawMessage: true,
    16  }.Froze()
    17  
    18  type Container struct {
    19  	Bar interface{}
    20  }
    21  
    22  func (c *Container) MarshalJSON() ([]byte, error) {
    23  	return marshalConfig.Marshal(&c.Bar)
    24  }
    25  
    26  func TestEncodeEscape(t *testing.T) {
    27  	should := require.New(t)
    28  
    29  	container := &Container{
    30  		Bar: []string{"123<ab>", "ooo"},
    31  	}
    32  	out, err := marshalConfig.Marshal(container)
    33  	should.Nil(err)
    34  	bufout := string(out)
    35  
    36  	var stdbuf bytes.Buffer
    37  	stdenc := json.NewEncoder(&stdbuf)
    38  	stdenc.SetEscapeHTML(false)
    39  	err = stdenc.Encode(container)
    40  	should.Nil(err)
    41  	stdout := string(stdbuf.Bytes())
    42  	if stdout[len(stdout)-1:] == "\n" {
    43  		stdout = stdout[:len(stdout)-1]
    44  	}
    45  
    46  	should.Equal(stdout, bufout)
    47  }
    48  

View as plain text