Question:As far as I saw, I just did a customized
MarshalJSONmethod in order to replace these characters:
In the example above, i marshaled the similar struct by sending to marshal from an aux struct that contains the same fields and last step is to replace the fields that I actually need and the return. As you can see in the print placed before returning from
MarshalJSONmethod, the special characters were replaced, but after calling the
json.Marshalfunc, the special characters remains the same.
Something I’m missing here but cannot figure it out. Appreciate your help. Thankies 🙂
Marshaldocumentation of the json package https://pkg.go.dev/encoding/json#Marshal you will find the following paragraph:
String values encode as JSON strings coerced to valid UTF-8, replacing invalid bytes with the Unicode replacement rune. So that the JSON will be safe to embed inside HTML tags, the string is encoded using HTMLEscape, which replaces "<", ">", "&", U+2028, and U+2029 are escaped to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029". This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).
So try it using a
If you have better answer, please add a comment about this, thank you!