github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/abci/types/result.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"github.com/gogo/protobuf/jsonpb"
     7  )
     8  
     9  const (
    10  	CodeTypeOK uint32 = 0
    11  )
    12  
    13  // IsOK returns true if Code is OK.
    14  func (r ResponseCheckTx) IsOK() bool {
    15  	return r.Code == CodeTypeOK
    16  }
    17  
    18  // IsErr returns true if Code is something other than OK.
    19  func (r ResponseCheckTx) IsErr() bool {
    20  	return r.Code != CodeTypeOK
    21  }
    22  
    23  //---------------------------------------------------------------------------
    24  // override JSON marshaling so we emit defaults (ie. disable omitempty)
    25  
    26  var (
    27  	jsonpbMarshaller = jsonpb.Marshaler{
    28  		EnumsAsInts:  true,
    29  		EmitDefaults: true,
    30  	}
    31  	jsonpbUnmarshaller = jsonpb.Unmarshaler{}
    32  )
    33  
    34  func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
    35  	s, err := jsonpbMarshaller.MarshalToString(r)
    36  	return []byte(s), err
    37  }
    38  
    39  func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
    40  	reader := bytes.NewBuffer(b)
    41  	return jsonpbUnmarshaller.Unmarshal(reader, r)
    42  }
    43  
    44  // Some compile time assertions to ensure we don't
    45  // have accidental runtime surprises later on.
    46  
    47  // jsonEncodingRoundTripper ensures that asserted
    48  // interfaces implement both MarshalJSON and UnmarshalJSON
    49  type jsonRoundTripper interface {
    50  	json.Marshaler
    51  	json.Unmarshaler
    52  }
    53  
    54  var _ jsonRoundTripper = (*ResponseCheckTx)(nil)