1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2015 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 "github.com/golang/protobuf/proto"
45 pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
46 proto3pb "github.com/golang/protobuf/proto/proto3_proto"
47 "github.com/golang/protobuf/ptypes"
48 anypb "github.com/golang/protobuf/ptypes/any"
49 durpb "github.com/golang/protobuf/ptypes/duration"
50 stpb "github.com/golang/protobuf/ptypes/struct"
51 tspb "github.com/golang/protobuf/ptypes/timestamp"
52 wpb "github.com/golang/protobuf/ptypes/wrappers"
56 marshaler = Marshaler{}
58 marshalerAllOptions = Marshaler{
62 simpleObject = &pb.Simple{
63 OInt32: proto.Int32(-32),
64 OInt64: proto.Int64(-6400000000),
65 OUint32: proto.Uint32(32),
66 OUint64: proto.Uint64(6400000000),
67 OSint32: proto.Int32(-13),
68 OSint64: proto.Int64(-2600000000),
69 OFloat: proto.Float32(3.14),
70 ODouble: proto.Float64(6.02214179e23),
71 OBool: proto.Bool(true),
72 OString: proto.String("hello \"there\""),
73 OBytes: []byte("beep boop"),
76 simpleObjectJSON = `{` +
79 `"oInt64":"-6400000000",` +
81 `"oUint64":"6400000000",` +
83 `"oSint64":"-2600000000",` +
85 `"oDouble":6.02214179e+23,` +
86 `"oString":"hello \"there\"",` +
87 `"oBytes":"YmVlcCBib29w"` +
90 simpleObjectPrettyJSON = `{
93 "oInt64": "-6400000000",
95 "oUint64": "6400000000",
97 "oSint64": "-2600000000",
99 "oDouble": 6.02214179e+23,
100 "oString": "hello \"there\"",
101 "oBytes": "YmVlcCBib29w"
104 repeatsObject = &pb.Repeats{
105 RBool: []bool{true, false, true},
106 RInt32: []int32{-3, -4, -5},
107 RInt64: []int64{-123456789, -987654321},
108 RUint32: []uint32{1, 2, 3},
109 RUint64: []uint64{6789012345, 3456789012},
110 RSint32: []int32{-1, -2, -3},
111 RSint64: []int64{-6789012345, -3456789012},
112 RFloat: []float32{3.14, 6.28},
113 RDouble: []float64{299792458 * 1e20, 6.62606957e-34},
114 RString: []string{"happy", "days"},
115 RBytes: [][]byte{[]byte("skittles"), []byte("m&m's")},
118 repeatsObjectJSON = `{` +
119 `"rBool":[true,false,true],` +
120 `"rInt32":[-3,-4,-5],` +
121 `"rInt64":["-123456789","-987654321"],` +
122 `"rUint32":[1,2,3],` +
123 `"rUint64":["6789012345","3456789012"],` +
124 `"rSint32":[-1,-2,-3],` +
125 `"rSint64":["-6789012345","-3456789012"],` +
126 `"rFloat":[3.14,6.28],` +
127 `"rDouble":[2.99792458e+28,6.62606957e-34],` +
128 `"rString":["happy","days"],` +
129 `"rBytes":["c2tpdHRsZXM=","bSZtJ3M="]` +
132 repeatsObjectPrettyJSON = `{
183 innerSimple = &pb.Simple{OInt32: proto.Int32(-32)}
184 innerSimple2 = &pb.Simple{OInt64: proto.Int64(25)}
185 innerRepeats = &pb.Repeats{RString: []string{"roses", "red"}}
186 innerRepeats2 = &pb.Repeats{RString: []string{"violets", "blue"}}
187 complexObject = &pb.Widget{
188 Color: pb.Widget_GREEN.Enum(),
189 RColor: []pb.Widget_Color{pb.Widget_RED, pb.Widget_GREEN, pb.Widget_BLUE},
191 RSimple: []*pb.Simple{innerSimple, innerSimple2},
192 Repeats: innerRepeats,
193 RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
196 complexObjectJSON = `{"color":"GREEN",` +
197 `"rColor":["RED","GREEN","BLUE"],` +
198 `"simple":{"oInt32":-32},` +
199 `"rSimple":[{"oInt32":-32},{"oInt64":"25"}],` +
200 `"repeats":{"rString":["roses","red"]},` +
201 `"rRepeats":[{"rString":["roses","red"]},{"rString":["violets","blue"]}]` +
204 complexObjectPrettyJSON = `{
248 colorListPrettyJSON = `{
269 realNumber = &pb.Real{Value: proto.Float64(3.14159265359)}
270 realNumberName = "Pi"
271 complexNumber = &pb.Complex{Imaginary: proto.Float64(0.5772156649)}
272 realNumberJSON = `{` +
273 `"value":3.14159265359,` +
274 `"[jsonpb.Complex.real_extension]":{"imaginary":0.5772156649},` +
275 `"[jsonpb.name]":"Pi"` +
278 anySimple = &pb.KnownTypes{
280 TypeUrl: "something.example.com/jsonpb.Simple",
282 // &pb.Simple{OBool:true}
287 anySimpleJSON = `{"an":{"@type":"something.example.com/jsonpb.Simple","oBool":true}}`
288 anySimplePrettyJSON = `{
290 "@type": "something.example.com/jsonpb.Simple",
295 anyWellKnown = &pb.KnownTypes{
297 TypeUrl: "type.googleapis.com/google.protobuf.Duration",
299 // &durpb.Duration{Seconds: 1, Nanos: 212000000 }
300 1 << 3, 1, // seconds
301 2 << 3, 0x80, 0xba, 0x8b, 0x65, // nanos
305 anyWellKnownJSON = `{"an":{"@type":"type.googleapis.com/google.protobuf.Duration","value":"1.212s"}}`
306 anyWellKnownPrettyJSON = `{
308 "@type": "type.googleapis.com/google.protobuf.Duration",
313 nonFinites = &pb.NonFinites{
314 FNan: proto.Float32(float32(math.NaN())),
315 FPinf: proto.Float32(float32(math.Inf(1))),
316 FNinf: proto.Float32(float32(math.Inf(-1))),
317 DNan: proto.Float64(float64(math.NaN())),
318 DPinf: proto.Float64(float64(math.Inf(1))),
319 DNinf: proto.Float64(float64(math.Inf(-1))),
321 nonFinitesJSON = `{` +
323 `"fPinf":"Infinity",` +
324 `"fNinf":"-Infinity",` +
326 `"dPinf":"Infinity",` +
327 `"dNinf":"-Infinity"` +
332 if err := proto.SetExtension(realNumber, pb.E_Name, &realNumberName); err != nil {
335 if err := proto.SetExtension(realNumber, pb.E_Complex_RealExtension, complexNumber); err != nil {
340 var marshalingTests = []struct {
346 {"simple flat object", marshaler, simpleObject, simpleObjectJSON},
347 {"simple pretty object", marshalerAllOptions, simpleObject, simpleObjectPrettyJSON},
348 {"non-finite floats fields object", marshaler, nonFinites, nonFinitesJSON},
349 {"repeated fields flat object", marshaler, repeatsObject, repeatsObjectJSON},
350 {"repeated fields pretty object", marshalerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
351 {"nested message/enum flat object", marshaler, complexObject, complexObjectJSON},
352 {"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
353 {"enum-string flat object", Marshaler{},
354 &pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
355 {"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
356 &pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
357 {"unknown enum value object", marshalerAllOptions,
358 &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},
359 {"repeated proto3 enum", Marshaler{},
360 &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
361 proto3pb.Message_PUNS,
362 proto3pb.Message_SLAPSTICK,
364 `{"rFunny":["PUNS","SLAPSTICK"]}`},
365 {"repeated proto3 enum as int", Marshaler{EnumsAsInts: true},
366 &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
367 proto3pb.Message_PUNS,
368 proto3pb.Message_SLAPSTICK,
371 {"empty value", marshaler, &pb.Simple3{}, `{}`},
372 {"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
373 {"empty repeated emitted", Marshaler{EmitDefaults: true}, &pb.SimpleSlice3{}, `{"slices":[]}`},
374 {"empty map emitted", Marshaler{EmitDefaults: true}, &pb.SimpleMap3{}, `{"stringy":{}}`},
375 {"nested struct null", Marshaler{EmitDefaults: true}, &pb.SimpleNull3{}, `{"simple":null}`},
376 {"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
377 {"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
378 {"map<string, string>", marshaler,
379 &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
380 `{"strry":{"\"one\"":"two","three":"four"}}`},
381 {"map<int32, Object>", marshaler,
382 &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
383 {"map<int32, Object>", marshalerAllOptions,
384 &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, objjyPrettyJSON},
385 {"map<int64, string>", marshaler, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
386 `{"buggy":{"1234":"yup"}}`},
387 {"map<bool, bool>", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
388 // TODO: This is broken.
389 //{"map<string, enum>", marshaler, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":"ROMAN"}`},
390 {"map<string, enum as int>", Marshaler{EnumsAsInts: true}, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":2}}`},
391 {"map<int32, bool>", marshaler, &pb.Mappy{S32Booly: map[int32]bool{1: true, 3: false, 10: true, 12: false}}, `{"s32booly":{"1":true,"3":false,"10":true,"12":false}}`},
392 {"map<int64, bool>", marshaler, &pb.Mappy{S64Booly: map[int64]bool{1: true, 3: false, 10: true, 12: false}}, `{"s64booly":{"1":true,"3":false,"10":true,"12":false}}`},
393 {"map<uint32, bool>", marshaler, &pb.Mappy{U32Booly: map[uint32]bool{1: true, 3: false, 10: true, 12: false}}, `{"u32booly":{"1":true,"3":false,"10":true,"12":false}}`},
394 {"map<uint64, bool>", marshaler, &pb.Mappy{U64Booly: map[uint64]bool{1: true, 3: false, 10: true, 12: false}}, `{"u64booly":{"1":true,"3":false,"10":true,"12":false}}`},
395 {"proto2 map<int64, string>", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
396 `{"mInt64Str":{"213":"cat"}}`},
397 {"proto2 map<bool, Object>", marshaler,
398 &pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: {OInt32: proto.Int32(1)}}},
399 `{"mBoolSimple":{"true":{"oInt32":1}}}`},
400 {"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`},
401 {"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`},
402 {"force orig_name", Marshaler{OrigName: true}, &pb.Simple{OInt32: proto.Int32(4)},
404 {"proto2 extension", marshaler, realNumber, realNumberJSON},
405 {"Any with message", marshaler, anySimple, anySimpleJSON},
406 {"Any with message and indent", marshalerAllOptions, anySimple, anySimplePrettyJSON},
407 {"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON},
408 {"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON},
409 {"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3.000s"}`},
410 {"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{
411 Fields: map[string]*stpb.Value{
412 "one": {Kind: &stpb.Value_StringValue{"loneliest number"}},
413 "two": {Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}},
415 }}, `{"st":{"one":"loneliest number","two":null}}`},
416 {"empty ListValue", marshaler, &pb.KnownTypes{Lv: &stpb.ListValue{}}, `{"lv":[]}`},
417 {"basic ListValue", marshaler, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
418 {Kind: &stpb.Value_StringValue{"x"}},
419 {Kind: &stpb.Value_NullValue{}},
420 {Kind: &stpb.Value_NumberValue{3}},
421 {Kind: &stpb.Value_BoolValue{true}},
422 }}}, `{"lv":["x",null,3,true]}`},
423 {"Timestamp", marshaler, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}, `{"ts":"2014-05-13T16:53:20.021Z"}`},
424 {"number Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NumberValue{1}}}, `{"val":1}`},
425 {"null Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}}}, `{"val":null}`},
426 {"string number value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"9223372036854775807"}}}, `{"val":"9223372036854775807"}`},
427 {"list of lists Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{
428 Kind: &stpb.Value_ListValue{&stpb.ListValue{
429 Values: []*stpb.Value{
430 {Kind: &stpb.Value_StringValue{"x"}},
431 {Kind: &stpb.Value_ListValue{&stpb.ListValue{
432 Values: []*stpb.Value{
433 {Kind: &stpb.Value_ListValue{&stpb.ListValue{
434 Values: []*stpb.Value{{Kind: &stpb.Value_StringValue{"y"}}},
436 {Kind: &stpb.Value_StringValue{"z"}},
441 }}, `{"val":["x",[["y"],"z"]]}`},
443 {"DoubleValue", marshaler, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}, `{"dbl":1.2}`},
444 {"FloatValue", marshaler, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}, `{"flt":1.2}`},
445 {"Int64Value", marshaler, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}, `{"i64":"-3"}`},
446 {"UInt64Value", marshaler, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}, `{"u64":"3"}`},
447 {"Int32Value", marshaler, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}, `{"i32":-4}`},
448 {"UInt32Value", marshaler, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}, `{"u32":4}`},
449 {"BoolValue", marshaler, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}, `{"bool":true}`},
450 {"StringValue", marshaler, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}, `{"str":"plush"}`},
451 {"BytesValue", marshaler, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}, `{"bytes":"d293"}`},
454 func TestMarshaling(t *testing.T) {
455 for _, tt := range marshalingTests {
456 json, err := tt.marshaler.MarshalToString(tt.pb)
458 t.Errorf("%s: marshaling error: %v", tt.desc, err)
459 } else if tt.json != json {
460 t.Errorf("%s: got [%v] want [%v]", tt.desc, json, tt.json)
465 func TestMarshalJSONPBMarshaler(t *testing.T) {
466 rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
467 msg := dynamicMessage{rawJson: rawJson}
468 str, err := new(Marshaler).MarshalToString(&msg)
470 t.Errorf("an unexpected error occurred when marshalling JSONPBMarshaler: %v", err)
473 t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, rawJson)
477 func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
478 msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
479 a, err := ptypes.MarshalAny(&msg)
481 t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
483 str, err := new(Marshaler).MarshalToString(a)
485 t.Errorf("an unexpected error occurred when marshalling Any to JSON: %v", err)
487 // after custom marshaling, it's round-tripped through JSON decoding/encoding already,
488 // so the keys are sorted, whitespace is compacted, and "@type" key has been added
489 expected := `{"@type":"type.googleapis.com/` + dynamicMessageName + `","baz":[0,1,2,3],"foo":"bar"}`
491 t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, expected)
495 var unmarshalingTests = []struct {
497 unmarshaler Unmarshaler
501 {"simple flat object", Unmarshaler{}, simpleObjectJSON, simpleObject},
502 {"simple pretty object", Unmarshaler{}, simpleObjectPrettyJSON, simpleObject},
503 {"repeated fields flat object", Unmarshaler{}, repeatsObjectJSON, repeatsObject},
504 {"repeated fields pretty object", Unmarshaler{}, repeatsObjectPrettyJSON, repeatsObject},
505 {"nested message/enum flat object", Unmarshaler{}, complexObjectJSON, complexObject},
506 {"nested message/enum pretty object", Unmarshaler{}, complexObjectPrettyJSON, complexObject},
507 {"enum-string object", Unmarshaler{}, `{"color":"BLUE"}`, &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
508 {"enum-value object", Unmarshaler{}, "{\n \"color\": 2\n}", &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
509 {"unknown field with allowed option", Unmarshaler{AllowUnknownFields: true}, `{"unknown": "foo"}`, new(pb.Simple)},
510 {"proto3 enum string", Unmarshaler{}, `{"hilarity":"PUNS"}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
511 {"proto3 enum value", Unmarshaler{}, `{"hilarity":1}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
512 {"unknown enum value object",
514 "{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}",
515 &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}},
516 {"repeated proto3 enum", Unmarshaler{}, `{"rFunny":["PUNS","SLAPSTICK"]}`,
517 &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
518 proto3pb.Message_PUNS,
519 proto3pb.Message_SLAPSTICK,
521 {"repeated proto3 enum as int", Unmarshaler{}, `{"rFunny":[1,2]}`,
522 &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
523 proto3pb.Message_PUNS,
524 proto3pb.Message_SLAPSTICK,
526 {"repeated proto3 enum as mix of strings and ints", Unmarshaler{}, `{"rFunny":["PUNS",2]}`,
527 &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
528 proto3pb.Message_PUNS,
529 proto3pb.Message_SLAPSTICK,
531 {"unquoted int64 object", Unmarshaler{}, `{"oInt64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}},
532 {"unquoted uint64 object", Unmarshaler{}, `{"oUint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}},
533 {"NaN", Unmarshaler{}, `{"oDouble":"NaN"}`, &pb.Simple{ODouble: proto.Float64(math.NaN())}},
534 {"Inf", Unmarshaler{}, `{"oFloat":"Infinity"}`, &pb.Simple{OFloat: proto.Float32(float32(math.Inf(1)))}},
535 {"-Inf", Unmarshaler{}, `{"oDouble":"-Infinity"}`, &pb.Simple{ODouble: proto.Float64(math.Inf(-1))}},
536 {"map<int64, int32>", Unmarshaler{}, `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
537 {"map<string, string>", Unmarshaler{}, `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
538 {"map<int32, Object>", Unmarshaler{}, `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}},
539 {"proto2 extension", Unmarshaler{}, realNumberJSON, realNumber},
540 {"Any with message", Unmarshaler{}, anySimpleJSON, anySimple},
541 {"Any with message and indent", Unmarshaler{}, anySimplePrettyJSON, anySimple},
542 {"Any with WKT", Unmarshaler{}, anyWellKnownJSON, anyWellKnown},
543 {"Any with WKT and indent", Unmarshaler{}, anyWellKnownPrettyJSON, anyWellKnown},
544 // TODO: This is broken.
545 //{"map<string, enum>", Unmarshaler{}, `{"enumy":{"XIV":"ROMAN"}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
546 {"map<string, enum as int>", Unmarshaler{}, `{"enumy":{"XIV":2}}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
547 {"oneof", Unmarshaler{}, `{"salary":31000}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Salary{31000}}},
548 {"oneof spec name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
549 {"oneof orig_name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
550 {"oneof spec name2", Unmarshaler{}, `{"homeAddress":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
551 {"oneof orig_name2", Unmarshaler{}, `{"home_address":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
552 {"orig_name input", Unmarshaler{}, `{"o_bool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
553 {"camelName input", Unmarshaler{}, `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
555 {"Duration", Unmarshaler{}, `{"dur":"3.000s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
556 {"null Duration", Unmarshaler{}, `{"dur":null}`, &pb.KnownTypes{Dur: nil}},
557 {"Timestamp", Unmarshaler{}, `{"ts":"2014-05-13T16:53:20.021Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}},
558 {"PreEpochTimestamp", Unmarshaler{}, `{"ts":"1969-12-31T23:59:58.999999995Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -2, Nanos: 999999995}}},
559 {"ZeroTimeTimestamp", Unmarshaler{}, `{"ts":"0001-01-01T00:00:00Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -62135596800, Nanos: 0}}},
560 {"null Timestamp", Unmarshaler{}, `{"ts":null}`, &pb.KnownTypes{Ts: nil}},
561 {"null Struct", Unmarshaler{}, `{"st": null}`, &pb.KnownTypes{St: nil}},
562 {"empty Struct", Unmarshaler{}, `{"st": {}}`, &pb.KnownTypes{St: &stpb.Struct{}}},
563 {"basic Struct", Unmarshaler{}, `{"st": {"a": "x", "b": null, "c": 3, "d": true}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
564 "a": {Kind: &stpb.Value_StringValue{"x"}},
565 "b": {Kind: &stpb.Value_NullValue{}},
566 "c": {Kind: &stpb.Value_NumberValue{3}},
567 "d": {Kind: &stpb.Value_BoolValue{true}},
569 {"nested Struct", Unmarshaler{}, `{"st": {"a": {"b": 1, "c": [{"d": true}, "f"]}}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
570 "a": {Kind: &stpb.Value_StructValue{&stpb.Struct{Fields: map[string]*stpb.Value{
571 "b": {Kind: &stpb.Value_NumberValue{1}},
572 "c": {Kind: &stpb.Value_ListValue{&stpb.ListValue{Values: []*stpb.Value{
573 {Kind: &stpb.Value_StructValue{&stpb.Struct{Fields: map[string]*stpb.Value{"d": {Kind: &stpb.Value_BoolValue{true}}}}}},
574 {Kind: &stpb.Value_StringValue{"f"}},
578 {"null ListValue", Unmarshaler{}, `{"lv": null}`, &pb.KnownTypes{Lv: nil}},
579 {"empty ListValue", Unmarshaler{}, `{"lv": []}`, &pb.KnownTypes{Lv: &stpb.ListValue{}}},
580 {"basic ListValue", Unmarshaler{}, `{"lv": ["x", null, 3, true]}`, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
581 {Kind: &stpb.Value_StringValue{"x"}},
582 {Kind: &stpb.Value_NullValue{}},
583 {Kind: &stpb.Value_NumberValue{3}},
584 {Kind: &stpb.Value_BoolValue{true}},
586 {"number Value", Unmarshaler{}, `{"val":1}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NumberValue{1}}}},
587 {"null Value", Unmarshaler{}, `{"val":null}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}}}},
588 {"bool Value", Unmarshaler{}, `{"val":true}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_BoolValue{true}}}},
589 {"string Value", Unmarshaler{}, `{"val":"x"}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"x"}}}},
590 {"string number value", Unmarshaler{}, `{"val":"9223372036854775807"}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"9223372036854775807"}}}},
591 {"list of lists Value", Unmarshaler{}, `{"val":["x", [["y"], "z"]]}`, &pb.KnownTypes{Val: &stpb.Value{
592 Kind: &stpb.Value_ListValue{&stpb.ListValue{
593 Values: []*stpb.Value{
594 {Kind: &stpb.Value_StringValue{"x"}},
595 {Kind: &stpb.Value_ListValue{&stpb.ListValue{
596 Values: []*stpb.Value{
597 {Kind: &stpb.Value_ListValue{&stpb.ListValue{
598 Values: []*stpb.Value{{Kind: &stpb.Value_StringValue{"y"}}},
600 {Kind: &stpb.Value_StringValue{"z"}},
606 {"DoubleValue", Unmarshaler{}, `{"dbl":1.2}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}},
607 {"FloatValue", Unmarshaler{}, `{"flt":1.2}`, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}},
608 {"Int64Value", Unmarshaler{}, `{"i64":"-3"}`, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}},
609 {"UInt64Value", Unmarshaler{}, `{"u64":"3"}`, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}},
610 {"Int32Value", Unmarshaler{}, `{"i32":-4}`, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}},
611 {"UInt32Value", Unmarshaler{}, `{"u32":4}`, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}},
612 {"BoolValue", Unmarshaler{}, `{"bool":true}`, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}},
613 {"StringValue", Unmarshaler{}, `{"str":"plush"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}},
614 {"BytesValue", Unmarshaler{}, `{"bytes":"d293"}`, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}},
616 // Ensure that `null` as a value ends up with a nil pointer instead of a [type]Value struct.
617 {"null DoubleValue", Unmarshaler{}, `{"dbl":null}`, &pb.KnownTypes{Dbl: nil}},
618 {"null FloatValue", Unmarshaler{}, `{"flt":null}`, &pb.KnownTypes{Flt: nil}},
619 {"null Int64Value", Unmarshaler{}, `{"i64":null}`, &pb.KnownTypes{I64: nil}},
620 {"null UInt64Value", Unmarshaler{}, `{"u64":null}`, &pb.KnownTypes{U64: nil}},
621 {"null Int32Value", Unmarshaler{}, `{"i32":null}`, &pb.KnownTypes{I32: nil}},
622 {"null UInt32Value", Unmarshaler{}, `{"u32":null}`, &pb.KnownTypes{U32: nil}},
623 {"null BoolValue", Unmarshaler{}, `{"bool":null}`, &pb.KnownTypes{Bool: nil}},
624 {"null StringValue", Unmarshaler{}, `{"str":null}`, &pb.KnownTypes{Str: nil}},
625 {"null BytesValue", Unmarshaler{}, `{"bytes":null}`, &pb.KnownTypes{Bytes: nil}},
628 func TestUnmarshaling(t *testing.T) {
629 for _, tt := range unmarshalingTests {
630 // Make a new instance of the type of our expected object.
631 p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
633 err := tt.unmarshaler.Unmarshal(strings.NewReader(tt.json), p)
635 t.Errorf("%s: %v", tt.desc, err)
639 // For easier diffs, compare text strings of the protos.
640 exp := proto.MarshalTextString(tt.pb)
641 act := proto.MarshalTextString(p)
642 if string(exp) != string(act) {
643 t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
648 func TestUnmarshalNullArray(t *testing.T) {
649 var repeats pb.Repeats
650 if err := UnmarshalString(`{"rBool":null}`, &repeats); err != nil {
653 if !reflect.DeepEqual(repeats, pb.Repeats{}) {
654 t.Errorf("got non-nil fields in [%#v]", repeats)
658 func TestUnmarshalNullObject(t *testing.T) {
660 if err := UnmarshalString(`{"mInt64Str":null}`, &maps); err != nil {
663 if !reflect.DeepEqual(maps, pb.Maps{}) {
664 t.Errorf("got non-nil fields in [%#v]", maps)
668 func TestUnmarshalNext(t *testing.T) {
669 // We only need to check against a few, not all of them.
670 tests := unmarshalingTests[:5]
672 // Create a buffer with many concatenated JSON objects.
674 for _, tt := range tests {
675 b.WriteString(tt.json)
678 dec := json.NewDecoder(&b)
679 for _, tt := range tests {
680 // Make a new instance of the type of our expected object.
681 p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
683 err := tt.unmarshaler.UnmarshalNext(dec, p)
685 t.Errorf("%s: %v", tt.desc, err)
689 // For easier diffs, compare text strings of the protos.
690 exp := proto.MarshalTextString(tt.pb)
691 act := proto.MarshalTextString(p)
692 if string(exp) != string(act) {
693 t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
698 err := new(Unmarshaler).UnmarshalNext(dec, p)
700 t.Errorf("eof: got %v, expected io.EOF", err)
704 var unmarshalingShouldError = []struct {
709 {"a value", "666", new(pb.Simple)},
710 {"gibberish", "{adskja123;l23=-=", new(pb.Simple)},
711 {"unknown field", `{"unknown": "foo"}`, new(pb.Simple)},
712 {"unknown enum name", `{"hilarity":"DAVE"}`, new(proto3pb.Message)},
715 func TestUnmarshalingBadInput(t *testing.T) {
716 for _, tt := range unmarshalingShouldError {
717 err := UnmarshalString(tt.in, tt.pb)
719 t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
724 type funcResolver func(turl string) (proto.Message, error)
726 func (fn funcResolver) Resolve(turl string) (proto.Message, error) {
730 func TestAnyWithCustomResolver(t *testing.T) {
731 var resolvedTypeUrls []string
732 resolver := funcResolver(func(turl string) (proto.Message, error) {
733 resolvedTypeUrls = append(resolvedTypeUrls, turl)
734 return new(pb.Simple), nil
737 OBytes: []byte{1, 2, 3, 4},
738 OBool: proto.Bool(true),
739 OString: proto.String("foobar"),
740 OInt64: proto.Int64(1020304),
742 msgBytes, err := proto.Marshal(msg)
744 t.Errorf("an unexpected error occurred when marshaling message: %v", err)
746 // make an Any with a type URL that won't resolve w/out custom resolver
748 TypeUrl: "https://foobar.com/some.random.MessageKind",
752 m := Marshaler{AnyResolver: resolver}
753 js, err := m.MarshalToString(any)
755 t.Errorf("an unexpected error occurred when marshaling any to JSON: %v", err)
757 if len(resolvedTypeUrls) != 1 {
758 t.Errorf("custom resolver was not invoked during marshaling")
759 } else if resolvedTypeUrls[0] != "https://foobar.com/some.random.MessageKind" {
760 t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[0], "https://foobar.com/some.random.MessageKind")
762 wanted := `{"@type":"https://foobar.com/some.random.MessageKind","oBool":true,"oInt64":"1020304","oString":"foobar","oBytes":"AQIDBA=="}`
764 t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", js, wanted)
767 u := Unmarshaler{AnyResolver: resolver}
768 roundTrip := &anypb.Any{}
769 err = u.Unmarshal(bytes.NewReader([]byte(js)), roundTrip)
771 t.Errorf("an unexpected error occurred when unmarshaling any from JSON: %v", err)
773 if len(resolvedTypeUrls) != 2 {
774 t.Errorf("custom resolver was not invoked during marshaling")
775 } else if resolvedTypeUrls[1] != "https://foobar.com/some.random.MessageKind" {
776 t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[1], "https://foobar.com/some.random.MessageKind")
778 if !proto.Equal(any, roundTrip) {
779 t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", roundTrip, any)
783 func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
784 rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
785 var msg dynamicMessage
786 if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
787 t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
789 if msg.rawJson != rawJson {
790 t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.rawJson, rawJson)
794 func TestUnmarshalNullWithJSONPBUnmarshaler(t *testing.T) {
795 rawJson := `{"stringField":null}`
796 var ptrFieldMsg ptrFieldMessage
797 if err := Unmarshal(strings.NewReader(rawJson), &ptrFieldMsg); err != nil {
798 t.Errorf("unmarshal error: %v", err)
801 want := ptrFieldMessage{StringField: &stringField{IsSet: true, StringValue: "null"}}
802 if !proto.Equal(&ptrFieldMsg, &want) {
803 t.Errorf("unmarshal result StringField: got %v, want %v", ptrFieldMsg, want)
807 func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
808 rawJson := `{ "@type": "blah.com/` + dynamicMessageName + `", "foo": "bar", "baz": [0, 1, 2, 3] }`
810 if err := Unmarshal(strings.NewReader(rawJson), &got); err != nil {
811 t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
814 dm := &dynamicMessage{rawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
816 if b, err := proto.Marshal(dm); err != nil {
817 t.Errorf("an unexpected error occurred when marshaling message: %v", err)
819 want.TypeUrl = "blah.com/" + dynamicMessageName
823 if !proto.Equal(&got, &want) {
824 t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", got, want)
829 dynamicMessageName = "google.protobuf.jsonpb.testing.dynamicMessage"
833 // we register the custom type below so that we can use it in Any types
834 proto.RegisterType((*dynamicMessage)(nil), dynamicMessageName)
837 type ptrFieldMessage struct {
838 StringField *stringField `protobuf:"bytes,1,opt,name=stringField"`
841 func (m *ptrFieldMessage) Reset() {
844 func (m *ptrFieldMessage) String() string {
845 return m.StringField.StringValue
848 func (m *ptrFieldMessage) ProtoMessage() {
851 type stringField struct {
852 IsSet bool `protobuf:"varint,1,opt,name=isSet"`
853 StringValue string `protobuf:"bytes,2,opt,name=stringValue"`
856 func (s *stringField) Reset() {
859 func (s *stringField) String() string {
863 func (s *stringField) ProtoMessage() {
866 func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
868 s.StringValue = string(js)
872 // dynamicMessage implements protobuf.Message but is not a normal generated message type.
873 // It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
874 type dynamicMessage struct {
875 rawJson string `protobuf:"bytes,1,opt,name=rawJson"`
878 func (m *dynamicMessage) Reset() {
882 func (m *dynamicMessage) String() string {
886 func (m *dynamicMessage) ProtoMessage() {
889 func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
890 return []byte(m.rawJson), nil
893 func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
894 m.rawJson = string(js)