FAQ
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.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Johann Höchtl at Feb 27, 2014 at 1:24 pm

    Am Donnerstag, 27. Februar 2014 08:05:10 UTC+1 schrieb Johann Höchtl:
    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.
    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

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Aroman at Feb 27, 2014 at 7:49 pm
    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
    On Thursday, February 27, 2014 5:24:24 AM UTC-8, Johann Höchtl wrote:



    Am Donnerstag, 27. Februar 2014 08:05:10 UTC+1 schrieb Johann Höchtl:
    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.
    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
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Benjamin Measures at Feb 28, 2014 at 11:45 pm

    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.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Harden at Mar 1, 2014 at 3:26 pm

    On Fri, Feb 28, 2014 at 5:45 PM, Benjamin Measures wrote:
    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.
    Why? What's important about it?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 27, '14 at 7:05a
activeMar 1, '14 at 3:26p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase