json RawMessage and interface{}

1,102 views
Skip to first unread message

Johann Höchtl

unread,
Feb 27, 2014, 2:05:10 AM2/27/14
to golan...@googlegroups.com
According to the docs, if the Marshaller of json hits a json.RawMessage struct member, it won't apply encoding heurisitics, that's at least my understanding. Following this thread it works:
http://play.golang.org/p/b8fzGMxrMC

But what if the struct member is of type interface{}, according to the documentation, "Interface values encode as the value contained in the interface." So I thought that member Data of a struct defined as

type S struct {
    Data interface{}
}

and assigned like that

Data: json.RawMessage([]byte(`{"Blah":"42"}`))

it will still end up base64 encoded.

Johann Höchtl

unread,
Feb 27, 2014, 8:24:24 AM2/27/14
to golan...@googlegroups.com
As it turns out I have indeed been hit by the json RawMessage pointer receiver inconvenience.

This doesnt' work: http://play.golang.org/p/HmvqdQWLLm
This works: http://play.golang.org/p/6FgwQEWj69

aro...@gmail.com

unread,
Feb 27, 2014, 2:47:20 PM2/27/14
to golan...@googlegroups.com
For the record, I vote that this is considered a bug and be fixed in go 1.x.  I can't see any reasonable usage that exploits the non-pointer receiver functionality intentionally, and it's VASTLY more likely to be a recurring source of bugs.

- Augusto

Benjamin Measures

unread,
Feb 28, 2014, 6:45:44 PM2/28/14
to golan...@googlegroups.com, aro...@gmail.com
On Thursday, 27 February 2014 19:47:20 UTC, aro...@gmail.com wrote:
For the record, I vote that this is considered a bug and be fixed in go 1.x.  I can't see any reasonable usage that exploits the non-pointer receiver functionality intentionally, and it's VASTLY more likely to be a recurring source of bugs.

That MarshalJSON has a pointer receiver is consistent with UnmarshalJSON. This is important.

Matt Harden

unread,
Mar 1, 2014, 10:25:51 AM3/1/14
to Benjamin Measures, golang-nuts, Augusto Roman
Why? What's important about it?

Reply all
Reply to author
Forward
0 new messages