Standard library / encoding/json
encoding/json.RawMessage.MarshalJSON()
Return the stored raw JSON bytes; nil becomes JSON null. Direct calls do not validate non-nil bytes, while json.Marshal validates the result. Expected stdout: '{"x":1}\n'.
Formal Syntax & Structure
go syntaxgo
func (m RawMessage) MarshalJSON() ([]byte, error)Return Value
([]byte, error)
Encoded bytes and an error (nil on success).
Working Example
Example Snippetgo
package main
import ( "encoding/json" "fmt")
func must(e error) { if e != nil { panic(e) }}
func main() { b, e := json.RawMessage(`{"x":1}`).MarshalJSON(); must(e); fmt.Println(string(b)) }