ffjson icon indicating copy to clipboard operation
ffjson copied to clipboard

pass escapeHTML flag through generated code

Open jrmarkle opened this issue 7 years ago • 10 comments

Possible fix for #237

jrmarkle avatar Oct 01 '18 19:10 jrmarkle

This is great, thank you!

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

pquerna avatar Oct 04 '18 10:10 pquerna

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 avatar Oct 04 '18 14:10 jrmarkle

can this get merged?

jrmarkle avatar Oct 15 '18 15:10 jrmarkle

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...?

dkegel-fastly avatar Apr 24 '21 22:04 dkegel-fastly

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)

jrmarkle avatar Apr 25 '21 01:04 jrmarkle

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

dkegel-fastly avatar Apr 25 '21 01:04 dkegel-fastly

(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 avatar Apr 25 '21 01:04 dkegel-fastly

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

dkegel-fastly avatar Apr 25 '21 01:04 dkegel-fastly

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

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

jrmarkle avatar Apr 25 '21 01:04 jrmarkle

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.

dkegel-fastly avatar Apr 25 '21 04:04 dkegel-fastly