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.

52 lines
1.2 KiB

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