github.com/koko1123/flow-go-1@v0.29.6/model/encoding/json/codec.go (about)

     1  package json
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  
     7  	"github.com/koko1123/flow-go-1/model/encoding"
     8  )
     9  
    10  var _ encoding.Marshaler = (*Marshaler)(nil)
    11  
    12  type Marshaler struct{}
    13  
    14  func NewMarshaler() *Marshaler {
    15  	return &Marshaler{}
    16  }
    17  
    18  func (m *Marshaler) Marshal(val interface{}) ([]byte, error) {
    19  	return json.Marshal(val)
    20  }
    21  
    22  func (m *Marshaler) Unmarshal(b []byte, val interface{}) error {
    23  	return json.Unmarshal(b, val)
    24  }
    25  
    26  func (m *Marshaler) MustMarshal(val interface{}) []byte {
    27  	b, err := m.Marshal(val)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  
    32  	return b
    33  }
    34  
    35  func (m *Marshaler) MustUnmarshal(b []byte, val interface{}) {
    36  	err := m.Unmarshal(b, val)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  }
    41  
    42  var _ encoding.Codec = (*Codec)(nil)
    43  
    44  type Codec struct{}
    45  
    46  func (c *Codec) NewEncoder(w io.Writer) encoding.Encoder {
    47  	return json.NewEncoder(w)
    48  }
    49  
    50  func (c *Codec) NewDecoder(r io.Reader) encoding.Decoder {
    51  	return json.NewDecoder(r)
    52  }