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.

43 lines
1.0 KiB

  1. // nolint: dupl
  2. // dupl is reading this as the same file as crypto/merkle/result.go
  3. package kv
  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 *Pair) MarshalJSON() ([]byte, error) {
  19. s, err := jsonpbMarshaller.MarshalToString(r)
  20. return []byte(s), err
  21. }
  22. func (r *Pair) UnmarshalJSON(b []byte) error {
  23. reader := bytes.NewBuffer(b)
  24. return jsonpbUnmarshaller.Unmarshal(reader, r)
  25. }
  26. // Some compile time assertions to ensure we don't
  27. // have accidental runtime surprises later on.
  28. // jsonEncodingRoundTripper ensures that asserted
  29. // interfaces implement both MarshalJSON and UnmarshalJSON
  30. type jsonRoundTripper interface {
  31. json.Marshaler
  32. json.Unmarshaler
  33. }
  34. var _ jsonRoundTripper = (*Pair)(nil)