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.

74 lines
1.8 KiB

  1. package proto3
  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 *PartSetHeader) MarshalJSON() ([]byte, error) {
  17. s, err := jsonpbMarshaller.MarshalToString(r)
  18. return []byte(s), err
  19. }
  20. func (r *PartSetHeader) UnmarshalJSON(b []byte) error {
  21. reader := bytes.NewBuffer(b)
  22. return jsonpbUnmarshaller.Unmarshal(reader, r)
  23. }
  24. func (r *Header) MarshalJSON() ([]byte, error) {
  25. s, err := jsonpbMarshaller.MarshalToString(r)
  26. return []byte(s), err
  27. }
  28. func (r *Header) UnmarshalJSON(b []byte) error {
  29. reader := bytes.NewBuffer(b)
  30. return jsonpbUnmarshaller.Unmarshal(reader, r)
  31. }
  32. func (r *Version) MarshalJSON() ([]byte, error) {
  33. s, err := jsonpbMarshaller.MarshalToString(r)
  34. return []byte(s), err
  35. }
  36. func (r *Version) UnmarshalJSON(b []byte) error {
  37. reader := bytes.NewBuffer(b)
  38. return jsonpbUnmarshaller.Unmarshal(reader, r)
  39. }
  40. func (r *Timestamp) MarshalJSON() ([]byte, error) {
  41. s, err := jsonpbMarshaller.MarshalToString(r)
  42. return []byte(s), err
  43. }
  44. func (r *Timestamp) UnmarshalJSON(b []byte) error {
  45. reader := bytes.NewBuffer(b)
  46. return jsonpbUnmarshaller.Unmarshal(reader, r)
  47. }
  48. // Some compile time assertions to ensure we don't
  49. // have accidental runtime surprises later on.
  50. // jsonEncodingRoundTripper ensures that asserted
  51. // interfaces implement both MarshalJSON and UnmarshalJSON
  52. type jsonRoundTripper interface {
  53. json.Marshaler
  54. json.Unmarshaler
  55. }
  56. var _ jsonRoundTripper = (*PartSetHeader)(nil)
  57. var _ jsonRoundTripper = (*Header)(nil)
  58. var _ jsonRoundTripper = (*Version)(nil)
  59. var _ jsonRoundTripper = (*Timestamp)(nil)