Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wln32 opened this issue May 7, 2024 · 0 comments
Open
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@wln32
Copy link
Member

wln32 commented May 7, 2024

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 wln32 added the bug It is confirmed a bug, but don't worry, we'll handle it. label May 7, 2024
@Issues-translate-bot Issues-translate-bot changed the title g.Dump: 实现了json.MarshalJSON的类型序列化时出错 g.Dump: An error occurred during type serialization of json.MarshalJSON May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

1 participant