github.com/argoproj/argo-cd@v1.8.7/util/grpc/json.go (about)

     1  package grpc
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  
     7  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
     8  )
     9  
    10  // JSONMarshaler is a type which satisfies the grpc-gateway Marshaler interface
    11  type JSONMarshaler struct{}
    12  
    13  // ContentType implements gwruntime.Marshaler.
    14  func (j *JSONMarshaler) ContentType() string {
    15  	return "application/json"
    16  }
    17  
    18  // Marshal implements gwruntime.Marshaler.
    19  func (j *JSONMarshaler) Marshal(v interface{}) ([]byte, error) {
    20  	return json.Marshal(v)
    21  }
    22  
    23  // NewDecoder implements gwruntime.Marshaler.
    24  func (j *JSONMarshaler) NewDecoder(r io.Reader) gwruntime.Decoder {
    25  	return json.NewDecoder(r)
    26  }
    27  
    28  // NewEncoder implements gwruntime.Marshaler.
    29  func (j *JSONMarshaler) NewEncoder(w io.Writer) gwruntime.Encoder {
    30  	return json.NewEncoder(w)
    31  }
    32  
    33  // Unmarshal implements gwruntime.Marshaler.
    34  func (j *JSONMarshaler) Unmarshal(data []byte, v interface{}) error {
    35  	return json.Unmarshal(data, v)
    36  }
    37  
    38  // MustMarshal is a convenience function to marshal an object successfully or panic
    39  func MustMarshal(v interface{}) []byte {
    40  	bytes, err := json.Marshal(v)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	return bytes
    45  }