英文:
Replace characters in go serialization by using custom MarshalJSON method
问题
根据我所看到的,我只是在自定义的MarshalJSON
方法中替换了这些字符:\u003c
和\u003e
:https://go.dev/play/p/xJ-qcMN9QXl
在上面的示例中,我通过将相似的结构体发送到辅助结构体中的marshal来进行编组,然后最后一步是替换我实际需要的字段并返回。
正如您在MarshalJSON
方法返回之前放置的打印输出中所看到的,特殊字符已被替换,但在调用json.Marshal
函数之后,特殊字符仍然保持不变。
我可能漏掉了一些东西,但无法弄清楚。感谢您的帮助。
谢谢
英文:
As far as I saw, I just did a customized MarshalJSON
method in order to replace these characters:\u003c
and \u003e
: https://go.dev/play/p/xJ-qcMN9QXl
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 MarshalJSON
method, the special characters were replaced, but after calling the json.Marshal
func, the special characters remains the same.
Something I'm missing here but cannot figure it out. Appreciate your help.
Thankies
答案1
得分: 2
在Marshal
文档的json包https://pkg.go.dev/encoding/json#Marshal中,您会找到以下段落:
字符串值被编码为强制转换为有效UTF-8的JSON字符串,将无效字节替换为Unicode替换符。为了使JSON在HTML
<script>
标签内嵌入安全,字符串使用HTMLEscape进行编码,将“<”、“>”、“&”、“U+2028”和“U+2029”转义为“\u003c”、“\u003e”、“\u0026”、“\u2028”和“\u2029”。使用编码器时,可以通过调用SetEscapeHTML(false)来禁用此替换。
所以尝试使用Encoder
,示例代码如下:
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type Foo struct {
Name string
Surname string
Likes map[string]interface{}
Hates map[string]interface{}
newGuy bool //rpcclonable
}
func main() {
foo := &Foo{
Name: "George",
Surname: "Denkin",
Likes: map[string]interface{}{
"Sports": "volleyball",
"Message": "<Geroge> play volleyball <usually>",
},
}
buf := &bytes.Buffer{} // or &strings.Builder{} as from the example of @mkopriva
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(foo)
if err != nil {
return
}
fmt.Println(buf.String())
}
英文:
In the Marshal
documentation 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 <script> 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 Encoder
, example:
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type Foo struct {
Name string
Surname string
Likes map[string]interface{}
Hates map[string]interface{}
newGuy bool //rpcclonable
}
func main() {
foo := &Foo{
Name: "George",
Surname: "Denkin",
Likes: map[string]interface{}{
"Sports": "volleyball",
"Message": "<Geroge> play volleyball <usually>",
},
}
buf := &bytes.Buffer{} // or &strings.Builder{} as from the example of @mkopriva
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(foo)
if err != nil {
return
}
fmt.Println(buf.String())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论