Skip to content

EscapeHTML=false doesn't work #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
teou opened this issue Aug 13, 2019 · 1 comment · Fixed by #398
Closed

EscapeHTML=false doesn't work #397

teou opened this issue Aug 13, 2019 · 1 comment · Fixed by #398

Comments

@teou
Copy link
Contributor

teou commented Aug 13, 2019

set EscapeHTML=false but the following test doesn't work:

package main

import (
        "bytes"
        "encoding/json"
        "fmt"
        "testing"

        jsoniter "github.com/json-iterator/go"
)

var marshalConfig = jsoniter.Config{
        EscapeHTML:             false,
        SortMapKeys:            true,
        ValidateJsonRawMessage: true,
}.Froze()

type Foo struct {
        Bar interface{}
}

func (f *Foo) MarshalJSON() ([]byte, error) {
        ret, err := marshalConfig.Marshal(&f.Bar)
        fmt.Println("MarshalJSON ret", string(ret), err)
        return ret, err
}

func TestFooEncode(t *testing.T) {
        foo := &Foo{
                Bar: []string{"123<ab>", "ooo"},
        }
        out, err := marshalConfig.Marshal(foo)
        if err != nil {
                t.Error("err must be nil")
                return
        }

        var stdbuf bytes.Buffer
        stdenc := json.NewEncoder(&stdbuf)
        stdenc.SetEscapeHTML(false)
        stdenc.Encode(foo)

        stdout := string(stdbuf.Bytes())
        if stdout[len(stdout)-1:] == "\n" {
                stdout = stdout[:len(stdout)-1]
        }
        bufout := string(out)
        fmt.Println("stdout", stdout)
        fmt.Println("bufout", bufout)
        if stdout != bufout {
                t.Error(fmt.Sprintf("should equal,std=%v,buf=%v", stdout, bufout))
        }
}

the output is :

MarshalJSON ret ["123<ab>","ooo"] <nil>
MarshalJSON ret ["123<ab>","ooo"] <nil>
stdout ["123<ab>","ooo"]
bufout ["123\u003cab\u003e","ooo"]
--- FAIL: TestFooEncode (0.00s)
    fail_test.go:52: should equal,std=["123<ab>","ooo"],buf=["123\u003cab\u003e","ooo"]
FAIL
exit status 1

we can see that the json was correctly marshaled in Foo.MarshalJSON, but then escaped again by standard json package?

i think pull request #337 may caused this issue.

@teou
Copy link
Contributor Author

teou commented Aug 15, 2019

@taowen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant