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.

41 lines
940 B

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