github.com/fufuok/utils@v1.0.10/xjson/xjson.go (about)

     1  package xjson
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  type RawMessage []byte
     8  
     9  // MarshalJSON returns m as the JSON encoding of m.
    10  func (m RawMessage) MarshalJSON() ([]byte, error) {
    11  	if m == nil {
    12  		return []byte("null"), nil
    13  	}
    14  	return m, nil
    15  }
    16  
    17  // UnmarshalJSON sets *m to a copy of data.
    18  func (m *RawMessage) UnmarshalJSON(data []byte) error {
    19  	if m == nil {
    20  		return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
    21  	}
    22  	*m = append((*m)[0:0], data...)
    23  	return nil
    24  }
    25  
    26  // Clone returns a copy of m.
    27  func (m RawMessage) Clone() []byte {
    28  	tmp := make([]byte, len(m))
    29  	copy(tmp, m)
    30  	return tmp
    31  }