Skip to content
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

SetEscapeHTML don't work for pointer types #237

Open
Hamper opened this issue Dec 25, 2017 · 1 comment
Open

SetEscapeHTML don't work for pointer types #237

Hamper opened this issue Dec 25, 2017 · 1 comment

Comments

@Hamper
Copy link

Hamper commented Dec 25, 2017

type T struct {
	A map[string]interface{}
	B map[string]string
	C string
	D Tx
	E *string
	F *Tx
}

type Tx struct {
	A map[string]interface{}
	B map[string]string
	C string
}

func main() {
	s := "test&test"
	x := T{
		A: map[string]interface{}{
			"test": "test&test",
		},
		B: map[string]string{
			"test": "test&test",
		},
		C: "test&test",
		D: Tx{
			A: map[string]interface{}{
				"test": "test&test",
			},
			B: map[string]string{
				"test": "test&test",
			},
			C: "test&test",
		},
		E: &s,
		F: &Tx{
			A: map[string]interface{}{
				"test": "test&test",
			},
			B: map[string]string{
				"test": "test&test",
			},
			C: "test&test",
		},
	}

	buffer := &bytes.Buffer{}
	encoder := json.NewEncoder(buffer)
	encoder.SetEscapeHTML(false)
	_ = encoder.Encode(x)
	fmt.Println(buffer.String())
}

With ffjson:

{"A":{"test":"test&test"},"B":{"test":"test&test"},"C":"test&test","D":{"A":{"test":"test&test"},"B":{"test":"test&test"},"C":"test&test"},"E":"test&test","F":{"A":{"test":"test\u0026test"},"B":{"test":"test\u0026test"},"C":"test\u0026test"}}

Without ffjson:

{"A":{"test":"test&test"},"B":{"test":"test&test"},"C":"test&test","D":{"A":{"test":"test&test"},"B":{"test":"test&test"},"C":"test&test"},"E":"test&test","F":{"A":{"test":"test&test"},"B":{"test":"test&test"},"C":"test&test"}}
@jrmarkle
Copy link

jrmarkle commented Oct 1, 2018

SetEscapeHTML() only calls through to the fall-back "encoding/json".Encoder from the standard library. The ffjson generated code always escapes the "HTML unsafe" characters.

If you encode a (non-pointer) struct it will use the fallback encoder because the generated code uses pointer receivers. If you want to use the fast ffjson encoder it will not respect the SetEscapeHTML() call.

I made PR #249 as a possible fix.

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

No branches or pull requests

2 participants