pquerna / ffjson

faster JSON serialization for Go
Apache License 2.0
2.97k stars 234 forks source link

pass escapeHTML flag through generated code #249

Open jrmarkle opened 6 years ago

jrmarkle commented 6 years ago

Possible fix for #237

pquerna commented 6 years ago

This is great, thank you!

My only concern is changing the marshalerFaster interface. I don't have an alternative though.

jrmarkle commented 6 years ago

It's an internal interface but yeah, the only alternative I see is to add the flag to fflib.EncodingBuffer somehow and that seemed like a worse option.

jrmarkle commented 6 years ago

can this get merged?

dkegel-fastly commented 3 years ago

I'm using

buf, err := ffjson.Marshal(&item)

and < is getting escaped in serialized values. I'd like to disable that, and this pull request looks apropos, but it doesn't seem to expose the escapeHTML flag through to the user...?

jrmarkle commented 3 years ago

I'm using

buf, err := ffjson.Marshal(&item)

and < is getting escaped in serialized values. I'd like to disable that, and this pull request looks apropos, but it doesn't seem to expose the escapeHTML flag through to the user...?

IIRC it should work the same way as the standard library. You need to create an encoder and configure it. eg:

buf := new(bytes.Buffer)
enc := ffjson.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.Encode(item)
dkegel-fastly commented 3 years ago

Doesn't that lose the speed advantage of ffjson.Marshal()...?

dkegel-fastly commented 3 years ago

(I wonder if a generation-time flag might be a good idea... then you wouldn't have to thread that option through as much, the generated code would just have true or false hardcoded. It'd work for my use case. And it would avoid changing that interface...?)

dkegel-fastly commented 3 years ago

https://github.com/pquerna/ffjson/issues/260 makes it a little hard to play around with this...

jrmarkle commented 3 years ago

Doesn't that lose the speed advantage of ffjson.Marshal()...?

Right, sorry. It should be ffjson.NewEncoder in the example (now corrected).

dkegel-fastly commented 3 years ago

I'm testing the change locally. To use my changed copy, I had to add this to go.mod in the app using ffjson:

replace github.com/pquerna/ffjson => /Users/dkegel/go/src/github.com/pquerna/ffjson

And it works! Well, kind of.

Oddly, even though I'm calling enc.SetEscapeHTML(false) both places I encode, in one case foo.MarshalJSON is being called, which hardcodes it to true.