|
|
@@ -44,6 +44,7 @@ import (
|
|
|
|
|
|
pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
|
|
|
proto3pb "github.com/golang/protobuf/proto/proto3_proto"
|
|
|
+ "github.com/golang/protobuf/ptypes"
|
|
|
anypb "github.com/golang/protobuf/ptypes/any"
|
|
|
durpb "github.com/golang/protobuf/ptypes/duration"
|
|
|
stpb "github.com/golang/protobuf/ptypes/struct"
|
|
|
@@ -461,7 +462,7 @@ func TestMarshaling(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func TestMarshalingWithJSONPBMarshaler(t *testing.T) {
|
|
|
+func TestMarshalJSONPBMarshaler(t *testing.T) {
|
|
|
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
|
|
|
msg := dynamicMessage{rawJson: rawJson}
|
|
|
str, err := new(Marshaler).MarshalToString(&msg)
|
|
|
@@ -473,6 +474,24 @@ func TestMarshalingWithJSONPBMarshaler(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
|
|
|
+ msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
|
|
|
+ a, err := ptypes.MarshalAny(&msg)
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
|
|
|
+ }
|
|
|
+ str, err := new(Marshaler).MarshalToString(a)
|
|
|
+ if err != nil {
|
|
|
+ t.Errorf("an unexpected error occurred when marshalling Any to JSON: %v", err)
|
|
|
+ }
|
|
|
+ // after custom marshaling, it's round-tripped through JSON decoding/encoding already,
|
|
|
+ // so the keys are sorted, whitespace is compacted, and "@type" key has been added
|
|
|
+ expected := `{"@type":"type.googleapis.com/` + dynamicMessageName +`","baz":[0,1,2,3],"foo":"bar"}`
|
|
|
+ if str != expected {
|
|
|
+ t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, expected)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
var unmarshalingTests = []struct {
|
|
|
desc string
|
|
|
unmarshaler Unmarshaler
|
|
|
@@ -670,11 +689,10 @@ func TestUnmarshalingBadInput(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func TestUnmarshalWithJSONPBUnmarshaler(t *testing.T) {
|
|
|
+func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
|
|
|
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
|
|
|
var msg dynamicMessage
|
|
|
- err := Unmarshal(strings.NewReader(rawJson), &msg)
|
|
|
- if err != nil {
|
|
|
+ if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
|
|
|
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
|
|
|
}
|
|
|
if msg.rawJson != rawJson {
|
|
|
@@ -682,10 +700,39 @@ func TestUnmarshalWithJSONPBUnmarshaler(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
|
|
|
+ rawJson := `{ "@type": "blah.com/` + dynamicMessageName + `", "foo": "bar", "baz": [0, 1, 2, 3] }`
|
|
|
+ var got anypb.Any
|
|
|
+ if err := Unmarshal(strings.NewReader(rawJson), &got); err != nil {
|
|
|
+ t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ dm := &dynamicMessage{rawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
|
|
|
+ var want anypb.Any
|
|
|
+ if b, err := proto.Marshal(dm); err != nil {
|
|
|
+ t.Errorf("an unexpected error occurred when marshaling message: %v", err)
|
|
|
+ } else {
|
|
|
+ want.TypeUrl = "blah.com/" + dynamicMessageName
|
|
|
+ want.Value = b
|
|
|
+ }
|
|
|
+
|
|
|
+ if !proto.Equal(&got, &want) {
|
|
|
+ t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", got, want)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const (
|
|
|
+ dynamicMessageName = "google.protobuf.jsonpb.testing.dynamicMessage"
|
|
|
+)
|
|
|
+func init() {
|
|
|
+ // we register the custom type below so that we can use it in Any types
|
|
|
+ proto.RegisterType((*dynamicMessage)(nil), dynamicMessageName)
|
|
|
+}
|
|
|
+
|
|
|
// dynamicMessage implements protobuf.Message but is not a normal generated message type.
|
|
|
// It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
|
|
|
type dynamicMessage struct {
|
|
|
- rawJson string
|
|
|
+ rawJson string `protobuf:"bytes,1,opt,name=rawJson"`
|
|
|
}
|
|
|
|
|
|
func (m *dynamicMessage) Reset() {
|
|
|
@@ -703,7 +750,7 @@ func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
|
|
|
return []byte(m.rawJson), nil
|
|
|
}
|
|
|
|
|
|
-func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, json []byte) error {
|
|
|
- m.rawJson = string(json)
|
|
|
+func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
|
|
|
+ m.rawJson = string(js)
|
|
|
return nil
|
|
|
-}
|
|
|
+}
|