|
@@ -74,6 +74,22 @@ type Marshaler struct {
|
|
|
OrigName bool
|
|
OrigName bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// JSONPBMarshaler is implemented by protobuf messages that customize the
|
|
|
|
|
+// way they are marshaled to JSON. Messages that implement this should
|
|
|
|
|
+// also implement JSONPBUnmarshaler so that the custom format can be
|
|
|
|
|
+// parsed.
|
|
|
|
|
+type JSONPBMarshaler interface {
|
|
|
|
|
+ MarshalJSONPB(*Marshaler) ([]byte, error)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// JSONPBUnmarshaler is implemented by protobuf messages that customize
|
|
|
|
|
+// the way they are unmarshaled from JSON. Messages that implement this
|
|
|
|
|
+// should also implement JSONPBMarshaler so that the custom format can be
|
|
|
|
|
+// produced.
|
|
|
|
|
+type JSONPBUnmarshaler interface {
|
|
|
|
|
+ UnmarshalJSONPB(*Unmarshaler, []byte) error
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Marshal marshals a protocol buffer into JSON.
|
|
// Marshal marshals a protocol buffer into JSON.
|
|
|
func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
|
|
func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
|
|
|
writer := &errWriter{writer: out}
|
|
writer := &errWriter{writer: out}
|
|
@@ -102,6 +118,15 @@ type wkt interface {
|
|
|
|
|
|
|
|
// marshalObject writes a struct to the Writer.
|
|
// marshalObject writes a struct to the Writer.
|
|
|
func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
|
|
func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
|
|
|
|
|
+ if jsm, ok := v.(JSONPBMarshaler); ok {
|
|
|
|
|
+ b, err := jsm.MarshalJSONPB(m)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ out.write(string(b))
|
|
|
|
|
+ return out.err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
s := reflect.ValueOf(v).Elem()
|
|
s := reflect.ValueOf(v).Elem()
|
|
|
|
|
|
|
|
// Handle well-known types.
|
|
// Handle well-known types.
|
|
@@ -571,6 +596,10 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|
|
return u.unmarshalValue(target.Elem(), inputValue, prop)
|
|
return u.unmarshalValue(target.Elem(), inputValue, prop)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if jsu, ok := target.Addr().Interface().(JSONPBUnmarshaler); ok {
|
|
|
|
|
+ return jsu.UnmarshalJSONPB(u, []byte(inputValue))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Handle well-known types.
|
|
// Handle well-known types.
|
|
|
type wkt interface {
|
|
type wkt interface {
|
|
|
XXX_WellKnownType() string
|
|
XXX_WellKnownType() string
|