@@ -74,6 +74,22 @@ type Marshaler struct {
74
74
OrigName bool
75
75
}
76
76
77
+ // JSONPBMarshaler is implemented by protobuf messages that customize the
78
+ // way they are marshaled to JSON. Messages that implement this should
79
+ // also implement JSONPBUnmarshaler so that the custom format can be
80
+ // parsed.
81
+ type JSONPBMarshaler interface {
82
+ MarshalJSONPB (* Marshaler ) ([]byte , error )
83
+ }
84
+
85
+ // JSONPBUnmarshaler is implemented by protobuf messages that customize
86
+ // the way they are unmarshaled from JSON. Messages that implement this
87
+ // should also implement JSONPBMarshaler so that the custom format can be
88
+ // produced.
89
+ type JSONPBUnmarshaler interface {
90
+ UnmarshalJSONPB (* Unmarshaler , []byte ) error
91
+ }
92
+
77
93
// Marshal marshals a protocol buffer into JSON.
78
94
func (m * Marshaler ) Marshal (out io.Writer , pb proto.Message ) error {
79
95
writer := & errWriter {writer : out }
@@ -102,8 +118,8 @@ type wkt interface {
102
118
103
119
// marshalObject writes a struct to the Writer.
104
120
func (m * Marshaler ) marshalObject (out * errWriter , v proto.Message , indent , typeURL string ) error {
105
- if jsm , ok := v .(json. Marshaler ); ok {
106
- b , err := jsm .MarshalJSON ( )
121
+ if jsm , ok := v .(JSONPBMarshaler ); ok {
122
+ b , err := jsm .MarshalJSONPB ( m )
107
123
if err != nil {
108
124
return err
109
125
}
@@ -537,8 +553,8 @@ func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
537
553
if err := dec .Decode (& inputValue ); err != nil {
538
554
return err
539
555
}
540
- if jsu , ok := pb .(json. Unmarshaler ); ok {
541
- return jsu .UnmarshalJSON ( []byte (inputValue ))
556
+ if jsu , ok := pb .(JSONPBUnmarshaler ); ok {
557
+ return jsu .UnmarshalJSONPB ( u , []byte (inputValue ))
542
558
}
543
559
return u .unmarshalValue (reflect .ValueOf (pb ).Elem (), inputValue , nil )
544
560
}
0 commit comments