jsonpb_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2015 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  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
  15. // distribution.
  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.
  19. //
  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.
  31. package jsonpb
  32. import (
  33. "bytes"
  34. "encoding/json"
  35. "io"
  36. "reflect"
  37. "testing"
  38. "github.com/golang/protobuf/proto"
  39. pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
  40. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  41. anypb "github.com/golang/protobuf/ptypes/any"
  42. durpb "github.com/golang/protobuf/ptypes/duration"
  43. stpb "github.com/golang/protobuf/ptypes/struct"
  44. tspb "github.com/golang/protobuf/ptypes/timestamp"
  45. wpb "github.com/golang/protobuf/ptypes/wrappers"
  46. )
  47. var (
  48. marshaler = Marshaler{}
  49. marshalerAllOptions = Marshaler{
  50. Indent: " ",
  51. }
  52. simpleObject = &pb.Simple{
  53. OInt32: proto.Int32(-32),
  54. OInt64: proto.Int64(-6400000000),
  55. OUint32: proto.Uint32(32),
  56. OUint64: proto.Uint64(6400000000),
  57. OSint32: proto.Int32(-13),
  58. OSint64: proto.Int64(-2600000000),
  59. OFloat: proto.Float32(3.14),
  60. ODouble: proto.Float64(6.02214179e23),
  61. OBool: proto.Bool(true),
  62. OString: proto.String("hello \"there\""),
  63. OBytes: []byte("beep boop"),
  64. }
  65. simpleObjectJSON = `{` +
  66. `"oBool":true,` +
  67. `"oInt32":-32,` +
  68. `"oInt64":"-6400000000",` +
  69. `"oUint32":32,` +
  70. `"oUint64":"6400000000",` +
  71. `"oSint32":-13,` +
  72. `"oSint64":"-2600000000",` +
  73. `"oFloat":3.14,` +
  74. `"oDouble":6.02214179e+23,` +
  75. `"oString":"hello \"there\"",` +
  76. `"oBytes":"YmVlcCBib29w"` +
  77. `}`
  78. simpleObjectPrettyJSON = `{
  79. "oBool": true,
  80. "oInt32": -32,
  81. "oInt64": "-6400000000",
  82. "oUint32": 32,
  83. "oUint64": "6400000000",
  84. "oSint32": -13,
  85. "oSint64": "-2600000000",
  86. "oFloat": 3.14,
  87. "oDouble": 6.02214179e+23,
  88. "oString": "hello \"there\"",
  89. "oBytes": "YmVlcCBib29w"
  90. }`
  91. repeatsObject = &pb.Repeats{
  92. RBool: []bool{true, false, true},
  93. RInt32: []int32{-3, -4, -5},
  94. RInt64: []int64{-123456789, -987654321},
  95. RUint32: []uint32{1, 2, 3},
  96. RUint64: []uint64{6789012345, 3456789012},
  97. RSint32: []int32{-1, -2, -3},
  98. RSint64: []int64{-6789012345, -3456789012},
  99. RFloat: []float32{3.14, 6.28},
  100. RDouble: []float64{299792458, 6.62606957e-34},
  101. RString: []string{"happy", "days"},
  102. RBytes: [][]byte{[]byte("skittles"), []byte("m&m's")},
  103. }
  104. repeatsObjectJSON = `{` +
  105. `"rBool":[true,false,true],` +
  106. `"rInt32":[-3,-4,-5],` +
  107. `"rInt64":["-123456789","-987654321"],` +
  108. `"rUint32":[1,2,3],` +
  109. `"rUint64":["6789012345","3456789012"],` +
  110. `"rSint32":[-1,-2,-3],` +
  111. `"rSint64":["-6789012345","-3456789012"],` +
  112. `"rFloat":[3.14,6.28],` +
  113. `"rDouble":[2.99792458e+08,6.62606957e-34],` +
  114. `"rString":["happy","days"],` +
  115. `"rBytes":["c2tpdHRsZXM=","bSZtJ3M="]` +
  116. `}`
  117. repeatsObjectPrettyJSON = `{
  118. "rBool": [
  119. true,
  120. false,
  121. true
  122. ],
  123. "rInt32": [
  124. -3,
  125. -4,
  126. -5
  127. ],
  128. "rInt64": [
  129. "-123456789",
  130. "-987654321"
  131. ],
  132. "rUint32": [
  133. 1,
  134. 2,
  135. 3
  136. ],
  137. "rUint64": [
  138. "6789012345",
  139. "3456789012"
  140. ],
  141. "rSint32": [
  142. -1,
  143. -2,
  144. -3
  145. ],
  146. "rSint64": [
  147. "-6789012345",
  148. "-3456789012"
  149. ],
  150. "rFloat": [
  151. 3.14,
  152. 6.28
  153. ],
  154. "rDouble": [
  155. 2.99792458e+08,
  156. 6.62606957e-34
  157. ],
  158. "rString": [
  159. "happy",
  160. "days"
  161. ],
  162. "rBytes": [
  163. "c2tpdHRsZXM=",
  164. "bSZtJ3M="
  165. ]
  166. }`
  167. innerSimple = &pb.Simple{OInt32: proto.Int32(-32)}
  168. innerSimple2 = &pb.Simple{OInt64: proto.Int64(25)}
  169. innerRepeats = &pb.Repeats{RString: []string{"roses", "red"}}
  170. innerRepeats2 = &pb.Repeats{RString: []string{"violets", "blue"}}
  171. complexObject = &pb.Widget{
  172. Color: pb.Widget_GREEN.Enum(),
  173. RColor: []pb.Widget_Color{pb.Widget_RED, pb.Widget_GREEN, pb.Widget_BLUE},
  174. Simple: innerSimple,
  175. RSimple: []*pb.Simple{innerSimple, innerSimple2},
  176. Repeats: innerRepeats,
  177. RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
  178. }
  179. complexObjectJSON = `{"color":"GREEN",` +
  180. `"rColor":["RED","GREEN","BLUE"],` +
  181. `"simple":{"oInt32":-32},` +
  182. `"rSimple":[{"oInt32":-32},{"oInt64":"25"}],` +
  183. `"repeats":{"rString":["roses","red"]},` +
  184. `"rRepeats":[{"rString":["roses","red"]},{"rString":["violets","blue"]}]` +
  185. `}`
  186. complexObjectPrettyJSON = `{
  187. "color": "GREEN",
  188. "rColor": [
  189. "RED",
  190. "GREEN",
  191. "BLUE"
  192. ],
  193. "simple": {
  194. "oInt32": -32
  195. },
  196. "rSimple": [
  197. {
  198. "oInt32": -32
  199. },
  200. {
  201. "oInt64": "25"
  202. }
  203. ],
  204. "repeats": {
  205. "rString": [
  206. "roses",
  207. "red"
  208. ]
  209. },
  210. "rRepeats": [
  211. {
  212. "rString": [
  213. "roses",
  214. "red"
  215. ]
  216. },
  217. {
  218. "rString": [
  219. "violets",
  220. "blue"
  221. ]
  222. }
  223. ]
  224. }`
  225. colorPrettyJSON = `{
  226. "color": 2
  227. }`
  228. colorListPrettyJSON = `{
  229. "color": 1000,
  230. "rColor": [
  231. "RED"
  232. ]
  233. }`
  234. nummyPrettyJSON = `{
  235. "nummy": {
  236. "1": 2,
  237. "3": 4
  238. }
  239. }`
  240. objjyPrettyJSON = `{
  241. "objjy": {
  242. "1": {
  243. "dub": 1
  244. }
  245. }
  246. }`
  247. realNumber = &pb.Real{Value: proto.Float64(3.14159265359)}
  248. realNumberName = "Pi"
  249. complexNumber = &pb.Complex{Imaginary: proto.Float64(0.5772156649)}
  250. realNumberJSON = `{` +
  251. `"value":3.14159265359,` +
  252. `"[jsonpb.Complex.real_extension]":{"imaginary":0.5772156649},` +
  253. `"[jsonpb.name]":"Pi"` +
  254. `}`
  255. )
  256. func init() {
  257. if err := proto.SetExtension(realNumber, pb.E_Name, &realNumberName); err != nil {
  258. panic(err)
  259. }
  260. if err := proto.SetExtension(realNumber, pb.E_Complex_RealExtension, complexNumber); err != nil {
  261. panic(err)
  262. }
  263. }
  264. var marshalingTests = []struct {
  265. desc string
  266. marshaler Marshaler
  267. pb proto.Message
  268. json string
  269. }{
  270. {"simple flat object", marshaler, simpleObject, simpleObjectJSON},
  271. {"simple pretty object", marshalerAllOptions, simpleObject, simpleObjectPrettyJSON},
  272. {"repeated fields flat object", marshaler, repeatsObject, repeatsObjectJSON},
  273. {"repeated fields pretty object", marshalerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
  274. {"nested message/enum flat object", marshaler, complexObject, complexObjectJSON},
  275. {"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
  276. {"enum-string flat object", Marshaler{},
  277. &pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
  278. {"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
  279. &pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
  280. {"unknown enum value object", marshalerAllOptions,
  281. &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},
  282. {"repeated proto3 enum", Marshaler{},
  283. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  284. proto3pb.Message_PUNS,
  285. proto3pb.Message_SLAPSTICK,
  286. }},
  287. `{"rFunny":["PUNS","SLAPSTICK"]}`},
  288. {"repeated proto3 enum as int", Marshaler{EnumsAsInts: true},
  289. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  290. proto3pb.Message_PUNS,
  291. proto3pb.Message_SLAPSTICK,
  292. }},
  293. `{"rFunny":[1,2]}`},
  294. {"empty value", marshaler, &pb.Simple3{}, `{}`},
  295. {"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
  296. {"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
  297. {"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
  298. {"map<string, string>", marshaler,
  299. &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
  300. `{"strry":{"\"one\"":"two","three":"four"}}`},
  301. {"map<int32, Object>", marshaler,
  302. &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
  303. {"map<int32, Object>", marshalerAllOptions,
  304. &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, objjyPrettyJSON},
  305. {"map<int64, string>", marshaler, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
  306. `{"buggy":{"1234":"yup"}}`},
  307. {"map<bool, bool>", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
  308. // TODO: This is broken.
  309. //{"map<string, enum>", marshaler, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":"ROMAN"}`},
  310. {"map<string, enum as int>", Marshaler{EnumsAsInts: true}, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":2}}`},
  311. {"proto2 map<int64, string>", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
  312. `{"mInt64Str":{"213":"cat"}}`},
  313. {"proto2 map<bool, Object>", marshaler,
  314. &pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: &pb.Simple{OInt32: proto.Int32(1)}}},
  315. `{"mBoolSimple":{"true":{"oInt32":1}}}`},
  316. {"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`},
  317. {"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`},
  318. {"force orig_name", Marshaler{OrigName: true}, &pb.Simple{OInt32: proto.Int32(4)},
  319. `{"o_int32":4}`},
  320. {"proto2 extension", marshaler, realNumber, realNumberJSON},
  321. {"Any with message", marshaler, &pb.KnownTypes{An: &anypb.Any{
  322. TypeUrl: "something.example.com/jsonpb.Simple",
  323. Value: []byte{
  324. // &pb.Simple{OBool:true}
  325. 1 << 3, 1,
  326. },
  327. }}, `{"an":{"@type":"something.example.com/jsonpb.Simple","oBool":true}}`},
  328. {"Any with WKT", marshaler, &pb.KnownTypes{An: &anypb.Any{
  329. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  330. Value: []byte{
  331. // &durpb.Duration{Seconds: 1, Nanos: 212000000 }
  332. 1 << 3, 1, // seconds
  333. 2 << 3, 0x80, 0xba, 0x8b, 0x65, // nanos
  334. },
  335. }}, `{"an":{"@type":"type.googleapis.com/google.protobuf.Duration","value":"1.212s"}}`},
  336. {"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3.000s"}`},
  337. {"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{
  338. Fields: map[string]*stpb.Value{
  339. "one": &stpb.Value{Kind: &stpb.Value_StringValue{"loneliest number"}},
  340. "two": &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}},
  341. },
  342. }}, `{"st":{"one":"loneliest number","two":null}}`},
  343. {"Timestamp", marshaler, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}, `{"ts":"2014-05-13T16:53:20.021Z"}`},
  344. {"DoubleValue", marshaler, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}, `{"dbl":1.2}`},
  345. {"FloatValue", marshaler, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}, `{"flt":1.2}`},
  346. {"Int64Value", marshaler, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}, `{"i64":"-3"}`},
  347. {"UInt64Value", marshaler, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}, `{"u64":"3"}`},
  348. {"Int32Value", marshaler, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}, `{"i32":-4}`},
  349. {"UInt32Value", marshaler, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}, `{"u32":4}`},
  350. {"BoolValue", marshaler, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}, `{"bool":true}`},
  351. {"StringValue", marshaler, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}, `{"str":"plush"}`},
  352. {"BytesValue", marshaler, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}, `{"bytes":"d293"}`},
  353. }
  354. func TestMarshaling(t *testing.T) {
  355. for _, tt := range marshalingTests {
  356. json, err := tt.marshaler.MarshalToString(tt.pb)
  357. if err != nil {
  358. t.Errorf("%s: marshaling error: %v", tt.desc, err)
  359. } else if tt.json != json {
  360. t.Errorf("%s: got [%v] want [%v]", tt.desc, json, tt.json)
  361. }
  362. }
  363. }
  364. var unmarshalingTests = []struct {
  365. desc string
  366. json string
  367. pb proto.Message
  368. }{
  369. {"simple flat object", simpleObjectJSON, simpleObject},
  370. {"simple pretty object", simpleObjectPrettyJSON, simpleObject},
  371. {"repeated fields flat object", repeatsObjectJSON, repeatsObject},
  372. {"repeated fields pretty object", repeatsObjectPrettyJSON, repeatsObject},
  373. {"nested message/enum flat object", complexObjectJSON, complexObject},
  374. {"nested message/enum pretty object", complexObjectPrettyJSON, complexObject},
  375. {"enum-string object", `{"color":"BLUE"}`, &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
  376. {"enum-value object", "{\n \"color\": 2\n}", &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
  377. {"proto3 enum string", `{"hilarity":"PUNS"}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  378. {"proto3 enum value", `{"hilarity":1}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  379. {"unknown enum value object",
  380. "{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}",
  381. &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}},
  382. {"repeated proto3 enum", `{"rFunny":["PUNS","SLAPSTICK"]}`,
  383. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  384. proto3pb.Message_PUNS,
  385. proto3pb.Message_SLAPSTICK,
  386. }}},
  387. {"repeated proto3 enum as int", `{"rFunny":[1,2]}`,
  388. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  389. proto3pb.Message_PUNS,
  390. proto3pb.Message_SLAPSTICK,
  391. }}},
  392. {"repeated proto3 enum as mix of strings and ints", `{"rFunny":["PUNS",2]}`,
  393. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  394. proto3pb.Message_PUNS,
  395. proto3pb.Message_SLAPSTICK,
  396. }}},
  397. {"unquoted int64 object", `{"oInt64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}},
  398. {"unquoted uint64 object", `{"oUint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}},
  399. {"map<int64, int32>", `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
  400. {"map<string, string>", `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
  401. {"map<int32, Object>", `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}},
  402. // TODO: This is broken.
  403. //{"map<string, enum>", `{"enumy":{"XIV":"ROMAN"}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
  404. {"map<string, enum as int>", `{"enumy":{"XIV":2}}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
  405. {"oneof", `{"salary":31000}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Salary{31000}}},
  406. {"oneof spec name", `{"country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
  407. {"oneof orig_name", `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
  408. {"orig_name input", `{"o_bool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
  409. {"camelName input", `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
  410. {"Duration", `{"dur":"3.000s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
  411. {"Timestamp", `{"ts":"2014-05-13T16:53:20.021Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}},
  412. {"DoubleValue", `{"dbl":1.2}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}},
  413. {"FloatValue", `{"flt":1.2}`, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}},
  414. {"Int64Value", `{"i64":"-3"}`, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}},
  415. {"UInt64Value", `{"u64":"3"}`, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}},
  416. {"Int32Value", `{"i32":-4}`, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}},
  417. {"UInt32Value", `{"u32":4}`, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}},
  418. {"BoolValue", `{"bool":true}`, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}},
  419. {"StringValue", `{"str":"plush"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}},
  420. {"BytesValue", `{"bytes":"d293"}`, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}},
  421. // `null` is also a permissible value. Let's just test one.
  422. {"null DoubleValue", `{"dbl":null}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{}}},
  423. }
  424. func TestUnmarshaling(t *testing.T) {
  425. for _, tt := range unmarshalingTests {
  426. // Make a new instance of the type of our expected object.
  427. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
  428. err := UnmarshalString(tt.json, p)
  429. if err != nil {
  430. t.Errorf("%s: %v", tt.desc, err)
  431. continue
  432. }
  433. // For easier diffs, compare text strings of the protos.
  434. exp := proto.MarshalTextString(tt.pb)
  435. act := proto.MarshalTextString(p)
  436. if string(exp) != string(act) {
  437. t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
  438. }
  439. }
  440. }
  441. func TestUnmarshalNext(t *testing.T) {
  442. // We only need to check against a few, not all of them.
  443. tests := unmarshalingTests[:5]
  444. // Create a buffer with many concatenated JSON objects.
  445. var b bytes.Buffer
  446. for _, tt := range tests {
  447. b.WriteString(tt.json)
  448. }
  449. dec := json.NewDecoder(&b)
  450. for _, tt := range tests {
  451. // Make a new instance of the type of our expected object.
  452. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
  453. err := UnmarshalNext(dec, p)
  454. if err != nil {
  455. t.Errorf("%s: %v", tt.desc, err)
  456. continue
  457. }
  458. // For easier diffs, compare text strings of the protos.
  459. exp := proto.MarshalTextString(tt.pb)
  460. act := proto.MarshalTextString(p)
  461. if string(exp) != string(act) {
  462. t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
  463. }
  464. }
  465. p := &pb.Simple{}
  466. err := UnmarshalNext(dec, p)
  467. if err != io.EOF {
  468. t.Errorf("eof: got %v, expected io.EOF", err)
  469. }
  470. }
  471. var unmarshalingShouldError = []struct {
  472. desc string
  473. in string
  474. pb proto.Message
  475. }{
  476. {"a value", "666", new(pb.Simple)},
  477. {"gibberish", "{adskja123;l23=-=", new(pb.Simple)},
  478. {"unknown enum name", `{"hilarity":"DAVE"}`, new(proto3pb.Message)},
  479. }
  480. func TestUnmarshalingBadInput(t *testing.T) {
  481. for _, tt := range unmarshalingShouldError {
  482. err := UnmarshalString(tt.in, tt.pb)
  483. if err == nil {
  484. t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
  485. }
  486. }
  487. }