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