gf icon indicating copy to clipboard operation
gf copied to clipboard

g.Dump: An error occurred during type serialization of json.MarshalJSON

Open wln32 opened this issue 1 year ago • 0 comments

Go version

1.22

GoFrame version

2.7.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

type implJson struct {
	buf []byte
}
func (i *implJson) MarshalJSON() ([]byte, error) {
	fmt.Println("implJson.MarshalJSON被调用")
	return i.buf, nil
}
type jsonResult struct {
	Id       int       `orm:"id"`
        // 1.指针类型时,正常调用MarshalJSON方法来序列化
       // 2值类型时,不会调用MarshalJSON方法来序列化
	JsonText *implJson `orm:"json_text"`
}

buf, err := json.Marshal(map[string]any{
		"test1": "1",
		"test2": 2,
		"test3": "gf",
})

var res =new(jsonResult)
res.Id = 2
res.JsonText  = &implJson{
		buf: buf,
}

What did you see happen?


// JsonText为指针类型时输出
g.Dump(res)
{
    Id:       2,
    JsonText: "{\"test1\":\"1\",\"test2\":2,\"test3\":\"gf\"}",
}

// 把JsonText 的类型改为值类型implJson 时,输出
g.Dump(res)
{
Id:       2,
    JsonText: {
        buf: [
            123,
            34,
            116,
            101,
           ...,省略后面的,实际上就是把这个buf当成一个个字符来输出,
        ],
    },
}

What did you expect to see?

{
    Id:       2,
    JsonText: "{\"test1\":\"1\",\"test2\":2,\"test3\":\"gf\"}",
}

{
    Id:       2,
    JsonText: "{\"test1\":\"1\",\"test2\":2,\"test3\":\"gf\"}",
}

wln32 avatar May 07 '24 11:05 wln32