I run the following code:
package main
import (
"encoding/json"
"fmt"
)
func main() {
raw := json.RawMessage(`{"foo":"bar"}`)
j, err := json.Marshal(raw)
if err != nil {
panic(err)
}
fmt.Println(string(j))
}
Playground: http://play.golang.org/p/qbkEIZRTPQ
Output:
"eyJmb28iOiJiYXIifQ=="
Desired output:
{"foo":"bar"}
Why does it base64 encode my RawMessage
as if it was an ordinary []byte
?
After all, RawMessage's implementation of MarshalJSON is just returning the byte slice
// MarshalJSON returns *m as the JSON encoding of m.
func (m *RawMessage) MarshalJSON() ([]byte, error) {
return *m, nil
}