Custom JSON Marshaller Supporting Base64 encoding | error calling MarshalJSON for type routes.Temp: invalid character 'e'

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

Custom JSON Marshaller Supporting Base64 encoding | error calling MarshalJSON for type routes.Temp: invalid character 'e'

问题

我想编写一个自定义的Marshaller。我已经按照以下方式实现了代码。

type Temp struct {
	Time time.Time
}

func (t Temp) MarshalJSON() ([]byte, error) {
	type __ Temp
	var x = __(t)
	var buff bytes.Buffer
	if err := json.NewEncoder(&buff).Encode(x); err != nil {
		return nil, err
	}
	raw := buff.Bytes()
	dst := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
	base64.StdEncoding.Encode(dst, raw)
	return dst, nil
}

路由逻辑如下。

func health(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"token": Temp{time.Now()},
	})
}

运行后,我得到了以下错误。

2022/08/06 00:36:53 [Recovery] 2022/08/06 - 00:36:53 panic recovered:
GET /health HTTP/1.1
Host: localhost:9000
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Sec-Ch-Ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36


json: error calling MarshalJSON for type routes.Temp: invalid character 'e' looking for beginning of value
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/render/json.go:56 (0x19448bd)
	JSON.Render: panic(err)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:915 (0x194fd3c)
	(*Context).Render: if err := r.Render(c.Writer); err != nil {
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:958 (0x19502f3)
	(*Context).JSON: c.Render(code, render.JSON{Data: obj})
/Users/vajahat/work/dotpe/order/routes/routes.go:43 (0x1f61b9c)
	health: c.JSON(200, gin.H{
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:165 (0x194a0bc)
	(*Context).Next: c.handlers[c.index](c)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/recovery.go:99 (0x195c437)
	CustomRecoveryWithWriter.func1: c.Next()
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:165 (0x194a0bc)
	(*Context).Next: c.handlers[c.index](c)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/logger.go:241 (0x195ae31)
	LoggerWithConfig.func1: c.Next()
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:165 (0x194a0bc)
	(*Context).Next: c.handlers[c.index](c)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/gin.go:489 (0x1958a7b)
	(*Engine).handleHTTPRequest: c.Next()
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/gin.go:445 (0x1958584)
	(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2947 (0x14ca5d3)
	serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1991 (0x14c413b)
	(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:1594 (0x1072640)
	goexit: BYTE	$0x90	// NOP

[GIN] 2022/08/06 - 00:36:53 | 500 |   5.20548072s |             ::1 | GET      "/health"

错误json: error calling MarshalJSON for type routes.Temp: invalid character 'e' looking for beginning of value困扰了我很长时间。

我期望的输出如下所示。

{
    "token":"eyJUaW1lIjoiMjAyMi0wOC0wNlQwMDo0ODoyOS4yNDEzNTUrMDU6MzAifQo="
}

在调试器中,它是正常工作的。
问题可能出现在渲染时,我希望得到上述输出。

非常感谢您的任何建议或帮助。

英文:

I want to write a custom Marshaller. I have done the implementations as Following.

type Temp struct {
	Time time.Time
}

func (t Temp) MarshalJSON() ([]byte, error) {
	type __ Temp
	var x = __(t)
	var buff bytes.Buffer
	if err := json.NewEncoder(&buff).Encode(x); err != nil {
		return nil, err
	}
	raw := buff.Bytes()
	dst := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
	base64.StdEncoding.Encode(dst, raw)
	return dst, nil
}

The router logic is as follow.

func health(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"token": Temp{time.Now()},
	})
}

After running, I get the error like.

2022/08/06 00:36:53 [Recovery] 2022/08/06 - 00:36:53 panic recovered:
GET /health HTTP/1.1
Host: localhost:9000
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Sec-Ch-Ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36


json: error calling MarshalJSON for type routes.Temp: invalid character 'e' looking for beginning of value
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/render/json.go:56 (0x19448bd)
	JSON.Render: panic(err)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:915 (0x194fd3c)
	(*Context).Render: if err := r.Render(c.Writer); err != nil {
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:958 (0x19502f3)
	(*Context).JSON: c.Render(code, render.JSON{Data: obj})
/Users/vajahat/work/dotpe/order/routes/routes.go:43 (0x1f61b9c)
	health: c.JSON(200, gin.H{
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:165 (0x194a0bc)
	(*Context).Next: c.handlers[c.index](c)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/recovery.go:99 (0x195c437)
	CustomRecoveryWithWriter.func1: c.Next()
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:165 (0x194a0bc)
	(*Context).Next: c.handlers[c.index](c)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/logger.go:241 (0x195ae31)
	LoggerWithConfig.func1: c.Next()
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/context.go:165 (0x194a0bc)
	(*Context).Next: c.handlers[c.index](c)
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/gin.go:489 (0x1958a7b)
	(*Engine).handleHTTPRequest: c.Next()
/Users/vajahat/go/pkg/mod/github.com/gin-gonic/gin@v1.7.0/gin.go:445 (0x1958584)
	(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2947 (0x14ca5d3)
	serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1991 (0x14c413b)
	(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:1594 (0x1072640)
	goexit: BYTE	$0x90	// NOP

[GIN] 2022/08/06 - 00:36:53 | 500 |   5.20548072s |             ::1 | GET      "/health"

The error json: error calling MarshalJSON for type routes.Temp: invalid character 'e' looking for beginning of value is troubling me for very long.

I am expecting an output as following.

{
    "token":"eyJUaW1lIjoiMjAyMi0wOC0wNlQwMDo0ODoyOS4yNDEzNTUrMDU6MzAifQo="
}

While in debugger, It is working.
The problem is at rendering time, i belive. I want to get above as output.

Any suggestion or help is highly appreciated.

Custom JSON Marshaller Supporting Base64 encoding | error calling MarshalJSON for type routes.Temp: invalid character 'e'

答案1

得分: 2

你的MarshalJSON()函数必须返回一个有效的JSON值;然而,你的示例生成了一个原始的Base64字符串,这不是有效的JSON。

你可以通过简单地在当前的返回值外面添加引号来修复它,使其成为一个有效的JSON字符串!

b64len := base64.StdEncoding.EncodedLen(len(raw))
dst := make([]byte, b64len+2)
base64.StdEncoding.Encode(dst[1:], raw)
dst[0], dst[b64len+1] = '"', '"'
return dst, nil
英文:

Your MarshalJSON() function must return a valid JSON value; however, your example produces a raw Base64 string, which is not valid JSON.

You can fix it by simply wrapping your current return value in quotes to make it a valid JSON string!

<!-- language: lang-go -->

b64len := base64.StdEncoding.EncodedLen(len(raw))
dst := make([]byte, b64len+2)
base64.StdEncoding.Encode(dst[1:], raw)
dst[0], dst[b64len+1] = &#39;&quot;&#39;, &#39;&quot;&#39;
return dst, nil

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

发表评论

匿名网友

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

确定