Golang encoding/json.RawMessage.UnmarshalJSON function example

package encoding/json

UnmarshalJSON sets *m to a copy of data.

Golang encoding/json.RawMessage.UnmarshalJSON function usage example

 func MarshalSlice(in []interface{}) ([]byte, error) {
  result := []*json.RawMessage{}
  for _, value := range in {
 bytes, err := Marshal(value)
 if err != nil {
 return nil, err
 }
 message := &json.RawMessage{} 
 message.UnmarshalJSON(bytes) // <--- here
 result = append(result, message)
  }
  return json.Marshal(result)
 }

References :

http://golang.org/pkg/encoding/json/#RawMessage.UnmarshalJSON

https://github.com/MongoHQ/mejson/blob/master/marshal.go

Advertisement