使用自定义的MarshalJSON方法来替换Go序列化中的字符。

huangapple go评论70阅读模式
英文:

Replace characters in go serialization by using custom MarshalJSON method

问题

根据我所看到的,我只是在自定义的MarshalJSON方法中替换了这些字符:\u003c\u003e:https://go.dev/play/p/xJ-qcMN9QXl

在上面的示例中,我通过将相似的结构体发送到辅助结构体中的marshal来进行编组,然后最后一步是替换我实际需要的字段并返回。
正如您在MarshalJSON方法返回之前放置的打印输出中所看到的,特殊字符已被替换,但在调用json.Marshal函数之后,特殊字符仍然保持不变。

我可能漏掉了一些东西,但无法弄清楚。感谢您的帮助。
谢谢 使用自定义的MarshalJSON方法来替换Go序列化中的字符。

英文:

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 使用自定义的MarshalJSON方法来替换Go序列化中的字符。

答案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 (
	&quot;bytes&quot;
	&quot;encoding/json&quot;
	&quot;fmt&quot;
)

type Foo struct {
	Name    string
	Surname string
	Likes   map[string]interface{}
	Hates   map[string]interface{}
	newGuy  bool //rpcclonable
}

func main() {
	foo := &amp;Foo{
		Name:    &quot;George&quot;,
		Surname: &quot;Denkin&quot;,
		Likes: map[string]interface{}{
			&quot;Sports&quot;:  &quot;volleyball&quot;,
			&quot;Message&quot;: &quot;&lt;Geroge&gt; play volleyball &lt;usually&gt;&quot;,
		},
	}
	buf := &amp;bytes.Buffer{} // or &amp;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())
}

huangapple
  • 本文由 发表于 2022年6月16日 22:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/72647252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定