You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/golang/protobuf/jsonpb"
  6. )
  7. //---------------------------------------------------------------------------
  8. // override JSON marshalling so we dont emit defaults (ie. disable omitempty)
  9. // note we need Unmarshal functions too because protobuf had the bright idea
  10. // to marshal int64->string. cool. cool, cool, cool: https://developers.google.com/protocol-buffers/docs/proto3#json
  11. var (
  12. jsonpbMarshaller = jsonpb.Marshaler{
  13. EnumsAsInts: true,
  14. EmitDefaults: false,
  15. }
  16. jsonpbUnmarshaller = jsonpb.Unmarshaler{}
  17. )
  18. func (r *ResponseSetOption) MarshalJSON() ([]byte, error) {
  19. s, err := jsonpbMarshaller.MarshalToString(r)
  20. return []byte(s), err
  21. }
  22. func (r *ResponseSetOption) UnmarshalJSON(b []byte) error {
  23. reader := bytes.NewBuffer(b)
  24. return jsonpbUnmarshaller.Unmarshal(reader, r)
  25. }
  26. func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
  27. s, err := jsonpbMarshaller.MarshalToString(r)
  28. return []byte(s), err
  29. }
  30. func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
  31. reader := bytes.NewBuffer(b)
  32. return jsonpbUnmarshaller.Unmarshal(reader, r)
  33. }
  34. func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
  35. s, err := jsonpbMarshaller.MarshalToString(r)
  36. return []byte(s), err
  37. }
  38. func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error {
  39. reader := bytes.NewBuffer(b)
  40. return jsonpbUnmarshaller.Unmarshal(reader, r)
  41. }
  42. func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
  43. s, err := jsonpbMarshaller.MarshalToString(r)
  44. return []byte(s), err
  45. }
  46. func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
  47. reader := bytes.NewBuffer(b)
  48. return jsonpbUnmarshaller.Unmarshal(reader, r)
  49. }
  50. func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
  51. s, err := jsonpbMarshaller.MarshalToString(r)
  52. return []byte(s), err
  53. }
  54. func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
  55. reader := bytes.NewBuffer(b)
  56. return jsonpbUnmarshaller.Unmarshal(reader, r)
  57. }
  58. // Some compile time assertions to ensure we don't
  59. // have accidental runtime surprises later on.
  60. // jsonEncodingRoundTripper ensures that asserted
  61. // interfaces implement both MarshalJSON and UnmarshalJSON
  62. type jsonRoundTripper interface {
  63. json.Marshaler
  64. json.Unmarshaler
  65. }
  66. var _ jsonRoundTripper = (*ResponseCommit)(nil)
  67. var _ jsonRoundTripper = (*ResponseQuery)(nil)
  68. var _ jsonRoundTripper = (*ResponseDeliverTx)(nil)
  69. var _ jsonRoundTripper = (*ResponseCheckTx)(nil)
  70. var _ jsonRoundTripper = (*ResponseSetOption)(nil)