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.

54 lines
1.3 KiB

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