Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.3k
-
I need to serve JSON responses that include full URLs containing query parameters. However, I'm encountering an issue where the URLs are being escaped—for example, characters like & and = in query parameters are converted to \u0026 and \u003d. In the Go standard library, json.Encoder.SetEscapeHTML(false) allows disabling this behavior. Is there a similar way to achieve this in Echo when using c.JSON(...), or do I need to manually handle the response to prevent escaping? |
BetaWas this translation helpful?Give feedback.
All reactions
It's possible to specify your ownJSONSerializer instead of the default one and setenc.SetEscapeHTML(false) during serialization:
e:=echo.New()e.JSONSerializer=&MyJSONSerializer{}...
// DefaultJSONSerializer implements JSON encoding using encoding/json.typeMyJSONSerializer echo.DefaultJSONSerializer// Serialize converts an interface into a json and writes it to the response.// You can optionally use the indent parameter to produce pretty JSONs.func (dMyJSONSerializer)Serialize(c echo.Context,iinterface{},indentstring)error {enc:=json.NewEncoder(c.Response())enc.SetEscapeHTML(false)ifindent!="" {enc.SetIndent("",indent)}returnenc.Encode(i)}// De…
Replies: 1 comment
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
It's possible to specify your own e:=echo.New()e.JSONSerializer=&MyJSONSerializer{}... // DefaultJSONSerializer implements JSON encoding using encoding/json.typeMyJSONSerializer echo.DefaultJSONSerializer// Serialize converts an interface into a json and writes it to the response.// You can optionally use the indent parameter to produce pretty JSONs.func (dMyJSONSerializer)Serialize(c echo.Context,iinterface{},indentstring)error {enc:=json.NewEncoder(c.Response())enc.SetEscapeHTML(false)ifindent!="" {enc.SetIndent("",indent)}returnenc.Encode(i)}// Deserialize reads a JSON from a request body and converts it into an interface.func (dMyJSONSerializer)Deserialize(c echo.Context,iinterface{})error {returnecho.DefaultJSONSerializer(d).Deserialize(c,i)} See how it''s implemented in Echo: |
BetaWas this translation helpful?Give feedback.