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.

53 lines
1.2 KiB

  1. // nolint: dupl
  2. package merkle
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "github.com/gogo/protobuf/jsonpb"
  7. )
  8. //---------------------------------------------------------------------------
  9. // override JSON marshalling so we emit defaults (ie. disable omitempty)
  10. var (
  11. jsonpbMarshaller = jsonpb.Marshaler{
  12. EnumsAsInts: true,
  13. EmitDefaults: true,
  14. }
  15. jsonpbUnmarshaller = jsonpb.Unmarshaler{}
  16. )
  17. func (r *ProofOp) MarshalJSON() ([]byte, error) {
  18. s, err := jsonpbMarshaller.MarshalToString(r)
  19. return []byte(s), err
  20. }
  21. func (r *ProofOp) UnmarshalJSON(b []byte) error {
  22. reader := bytes.NewBuffer(b)
  23. return jsonpbUnmarshaller.Unmarshal(reader, r)
  24. }
  25. func (r *Proof) MarshalJSON() ([]byte, error) {
  26. s, err := jsonpbMarshaller.MarshalToString(r)
  27. return []byte(s), err
  28. }
  29. func (r *Proof) UnmarshalJSON(b []byte) error {
  30. reader := bytes.NewBuffer(b)
  31. return jsonpbUnmarshaller.Unmarshal(reader, r)
  32. }
  33. // Some compile time assertions to ensure we don't
  34. // have accidental runtime surprises later on.
  35. // jsonEncodingRoundTripper ensures that asserted
  36. // interfaces implement both MarshalJSON and UnmarshalJSON
  37. type jsonRoundTripper interface {
  38. json.Marshaler
  39. json.Unmarshaler
  40. }
  41. var _ jsonRoundTripper = (*ProofOp)(nil)
  42. var _ jsonRoundTripper = (*Proof)(nil)