Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JSON without escaping#2769

Answeredbyevgenybf
qulxizer asked this question inQ&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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)}// 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:

You must be logged in to vote
0 replies
Answer selected byqulxizer
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@qulxizer@evgenybf

[8]ページ先頭

©2009-2026 Movatter.jp