在[]字节效果很好的时候无法元帅

发布于 2025-02-12 16:53:20 字数 1002 浏览 0 评论 0原文

可以在此处找到一个可重现的示例 https://go.dev/play/play/p/p/p/wnyhehezdfxvt

我想元帅(json.marshal(...)json.rawmessage字段。

type Container1 struct {
    OldValue json.RawMessage `json:"old"`
    NewValue json.RawMessage `json:"new"`
}

但是,它抱怨以下错误:

error calling MarshalJSON for type json.RawMessage: invalid character 'h' looking for beginning of value

更改json.rawmessage to []字节解决问题,但请注意,json.rawmessage是什么但是[]字节

// RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawMessage []byte

我还希望能够使用json.rawmessage,有帮助吗?谢谢!

A reproducible example can be found here https://go.dev/play/p/wNyhezDfxVt

I want to marshal (json.Marshal(...)) a struct with json.RawMessage fields.

type Container1 struct {
    OldValue json.RawMessage `json:"old"`
    NewValue json.RawMessage `json:"new"`
}

However, it complains about the below error:

error calling MarshalJSON for type json.RawMessage: invalid character 'h' looking for beginning of value

Changing json.RawMessage to []byte fix the issue, but note that json.RawMessage is nothing but []byte:

// RawMessage is a raw encoded JSON value.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawMessage []byte

I want to be still able to use json.RawMessage, any help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2025-02-19 16:53:20

类型JSON.RAWMESSAGE有望容纳有效的JSON,因为它将被序列化AS-IS(RAW)。

当您初始化JSON.RAWMESSAGE带有[] byte(“ Hello”)的字段时,您正在转换为[]字节,字符串文字“ Hello”,其内容为 hello 没有引号。然后json.marshal失败,因为您需要引号使其成为有效的JSON字符串。要保留引号Backticks

    c1 := Container1{
        []byte(`"hello"`),
        []byte(`"world"`),
    }

​仅仅是因为 json.marshal.marshal违约切片为base64。

实际上,修复了操场后,两个输出JSON看起来不同:

JSON.RAWMESSAGE

{"old":"hello","new":"world"}

来自[]字节

{"old":"aGVsbG8=","new":"d29ybGQ="}

The type json.RawMessage is expected to hold valid JSON, because it will be serialized as-is (raw).

When you initialize a json.RawMessage field with []byte("hello"), you're converting to []byte, the string literal "hello", whose content is hello without quotes. Then json.Marshal fails, because you need the quotes " for it to be a valid JSON string. To keep the quotes, you have to use an unescaped string literal enclosed in backticks.

Changing the initialization of your struct to this will work:

    c1 := Container1{
        []byte(`"hello"`),
        []byte(`"world"`),
    }

The []byte slice in Container2 instead doesn’t cause errors despite holding the same byte content simply because json.Marshal defaults to serializing byte slices as base64.

As a matter of fact, after fixing your playground, the two output JSON look different:

From json.RawMessage:

{"old":"hello","new":"world"}

From []byte:

{"old":"aGVsbG8=","new":"d29ybGQ="}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文