FAQ
I have this golang structure:

     type ErrorMessage struct {
         ServiceName string `json:service_name`
         HostName string `json:hostname`
         Message string `json:message`
         Extra json.RawMessage `json:extra`
         CriticalRate int `json:critical_rate`
     }

     type ErrorStored struct {
         ErrorMessage
         MessageId uuid.UUID
         Timestamp string
     }

     func handler(w http.ResponseWriter, r *http.Request) {
         e := getErrorLiteral()
         json.NewEncoder(w).Encode(e)
     }

However, the `Extra` field in `ErrorMessage` is not showing up correctly.
For instance, ``[]byte(`{"test": "stuff"}`)`` shows up as
`eyJ0ZXN0IjogInN0dWZmIn0=` in the json response.
What am I doing wrong? How do I get the json to show up as json?

Original Link:
http://stackoverflow.com/questions/30537300/golang-incompletely-encoding-a-struct-with-a-json-rawmessage-member

--
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/d/optout.

Search Discussions

  • James Bardin at May 29, 2015 at 8:19 pm
    I'm not sure if you figured out the answer since the SO question was
    deleted, but...
    On Friday, May 29, 2015 at 3:17:16 PM UTC-4, philne...@gmail.com wrote:


    However, the `Extra` field in `ErrorMessage` is not showing up correctly.
    For instance, ``[]byte(`{"test": "stuff"}`)`` shows up as
    `eyJ0ZXN0IjogInN0dWZmIn0=` in the json response.
    What am I doing wrong? How do I get the json to show up as json?
    You're getting a base64 encoding of the raw bytes, which is the default for
    []byte. The MarshalJSON and UnmarshalJSON methods are defined with a
    pointer receiver, so you need to use *json.RawMessage in order to get the
    behavior you want.


    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 29, '15 at 7:17p
activeMay 29, '15 at 8:19p
posts2
users2
websitegolang.org

2 users in discussion

Philneaton95: 1 post James Bardin: 1 post

People

Translate

site design / logo © 2021 Grokbase