jsonpb_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. "math"
  37. "reflect"
  38. "strings"
  39. "testing"
  40. "github.com/golang/protobuf/proto"
  41. pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
  42. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  43. "github.com/golang/protobuf/ptypes"
  44. anypb "github.com/golang/protobuf/ptypes/any"
  45. durpb "github.com/golang/protobuf/ptypes/duration"
  46. stpb "github.com/golang/protobuf/ptypes/struct"
  47. tspb "github.com/golang/protobuf/ptypes/timestamp"
  48. wpb "github.com/golang/protobuf/ptypes/wrappers"
  49. )
  50. var (
  51. marshaler = Marshaler{}
  52. marshalerAllOptions = Marshaler{
  53. Indent: " ",
  54. }
  55. simpleObject = &pb.Simple{
  56. OInt32: proto.Int32(-32),
  57. OInt64: proto.Int64(-6400000000),
  58. OUint32: proto.Uint32(32),
  59. OUint64: proto.Uint64(6400000000),
  60. OSint32: proto.Int32(-13),
  61. OSint64: proto.Int64(-2600000000),
  62. OFloat: proto.Float32(3.14),
  63. ODouble: proto.Float64(6.02214179e23),
  64. OBool: proto.Bool(true),
  65. OString: proto.String("hello \"there\""),
  66. OBytes: []byte("beep boop"),
  67. }
  68. simpleObjectJSON = `{` +
  69. `"oBool":true,` +
  70. `"oInt32":-32,` +
  71. `"oInt64":"-6400000000",` +
  72. `"oUint32":32,` +
  73. `"oUint64":"6400000000",` +
  74. `"oSint32":-13,` +
  75. `"oSint64":"-2600000000",` +
  76. `"oFloat":3.14,` +
  77. `"oDouble":6.02214179e+23,` +
  78. `"oString":"hello \"there\"",` +
  79. `"oBytes":"YmVlcCBib29w"` +
  80. `}`
  81. simpleObjectPrettyJSON = `{
  82. "oBool": true,
  83. "oInt32": -32,
  84. "oInt64": "-6400000000",
  85. "oUint32": 32,
  86. "oUint64": "6400000000",
  87. "oSint32": -13,
  88. "oSint64": "-2600000000",
  89. "oFloat": 3.14,
  90. "oDouble": 6.02214179e+23,
  91. "oString": "hello \"there\"",
  92. "oBytes": "YmVlcCBib29w"
  93. }`
  94. repeatsObject = &pb.Repeats{
  95. RBool: []bool{true, false, true},
  96. RInt32: []int32{-3, -4, -5},
  97. RInt64: []int64{-123456789, -987654321},
  98. RUint32: []uint32{1, 2, 3},
  99. RUint64: []uint64{6789012345, 3456789012},
  100. RSint32: []int32{-1, -2, -3},
  101. RSint64: []int64{-6789012345, -3456789012},
  102. RFloat: []float32{3.14, 6.28},
  103. RDouble: []float64{299792458 * 1e20, 6.62606957e-34},
  104. RString: []string{"happy", "days"},
  105. RBytes: [][]byte{[]byte("skittles"), []byte("m&m's")},
  106. }
  107. repeatsObjectJSON = `{` +
  108. `"rBool":[true,false,true],` +
  109. `"rInt32":[-3,-4,-5],` +
  110. `"rInt64":["-123456789","-987654321"],` +
  111. `"rUint32":[1,2,3],` +
  112. `"rUint64":["6789012345","3456789012"],` +
  113. `"rSint32":[-1,-2,-3],` +
  114. `"rSint64":["-6789012345","-3456789012"],` +
  115. `"rFloat":[3.14,6.28],` +
  116. `"rDouble":[2.99792458e+28,6.62606957e-34],` +
  117. `"rString":["happy","days"],` +
  118. `"rBytes":["c2tpdHRsZXM=","bSZtJ3M="]` +
  119. `}`
  120. repeatsObjectPrettyJSON = `{
  121. "rBool": [
  122. true,
  123. false,
  124. true
  125. ],
  126. "rInt32": [
  127. -3,
  128. -4,
  129. -5
  130. ],
  131. "rInt64": [
  132. "-123456789",
  133. "-987654321"
  134. ],
  135. "rUint32": [
  136. 1,
  137. 2,
  138. 3
  139. ],
  140. "rUint64": [
  141. "6789012345",
  142. "3456789012"
  143. ],
  144. "rSint32": [
  145. -1,
  146. -2,
  147. -3
  148. ],
  149. "rSint64": [
  150. "-6789012345",
  151. "-3456789012"
  152. ],
  153. "rFloat": [
  154. 3.14,
  155. 6.28
  156. ],
  157. "rDouble": [
  158. 2.99792458e+28,
  159. 6.62606957e-34
  160. ],
  161. "rString": [
  162. "happy",
  163. "days"
  164. ],
  165. "rBytes": [
  166. "c2tpdHRsZXM=",
  167. "bSZtJ3M="
  168. ]
  169. }`
  170. innerSimple = &pb.Simple{OInt32: proto.Int32(-32)}
  171. innerSimple2 = &pb.Simple{OInt64: proto.Int64(25)}
  172. innerRepeats = &pb.Repeats{RString: []string{"roses", "red"}}
  173. innerRepeats2 = &pb.Repeats{RString: []string{"violets", "blue"}}
  174. complexObject = &pb.Widget{
  175. Color: pb.Widget_GREEN.Enum(),
  176. RColor: []pb.Widget_Color{pb.Widget_RED, pb.Widget_GREEN, pb.Widget_BLUE},
  177. Simple: innerSimple,
  178. RSimple: []*pb.Simple{innerSimple, innerSimple2},
  179. Repeats: innerRepeats,
  180. RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
  181. }
  182. complexObjectJSON = `{"color":"GREEN",` +
  183. `"rColor":["RED","GREEN","BLUE"],` +
  184. `"simple":{"oInt32":-32},` +
  185. `"rSimple":[{"oInt32":-32},{"oInt64":"25"}],` +
  186. `"repeats":{"rString":["roses","red"]},` +
  187. `"rRepeats":[{"rString":["roses","red"]},{"rString":["violets","blue"]}]` +
  188. `}`
  189. complexObjectPrettyJSON = `{
  190. "color": "GREEN",
  191. "rColor": [
  192. "RED",
  193. "GREEN",
  194. "BLUE"
  195. ],
  196. "simple": {
  197. "oInt32": -32
  198. },
  199. "rSimple": [
  200. {
  201. "oInt32": -32
  202. },
  203. {
  204. "oInt64": "25"
  205. }
  206. ],
  207. "repeats": {
  208. "rString": [
  209. "roses",
  210. "red"
  211. ]
  212. },
  213. "rRepeats": [
  214. {
  215. "rString": [
  216. "roses",
  217. "red"
  218. ]
  219. },
  220. {
  221. "rString": [
  222. "violets",
  223. "blue"
  224. ]
  225. }
  226. ]
  227. }`
  228. colorPrettyJSON = `{
  229. "color": 2
  230. }`
  231. colorListPrettyJSON = `{
  232. "color": 1000,
  233. "rColor": [
  234. "RED"
  235. ]
  236. }`
  237. nummyPrettyJSON = `{
  238. "nummy": {
  239. "1": 2,
  240. "3": 4
  241. }
  242. }`
  243. objjyPrettyJSON = `{
  244. "objjy": {
  245. "1": {
  246. "dub": 1
  247. }
  248. }
  249. }`
  250. realNumber = &pb.Real{Value: proto.Float64(3.14159265359)}
  251. realNumberName = "Pi"
  252. complexNumber = &pb.Complex{Imaginary: proto.Float64(0.5772156649)}
  253. realNumberJSON = `{` +
  254. `"value":3.14159265359,` +
  255. `"[jsonpb.Complex.real_extension]":{"imaginary":0.5772156649},` +
  256. `"[jsonpb.name]":"Pi"` +
  257. `}`
  258. anySimple = &pb.KnownTypes{
  259. An: &anypb.Any{
  260. TypeUrl: "something.example.com/jsonpb.Simple",
  261. Value: []byte{
  262. // &pb.Simple{OBool:true}
  263. 1 << 3, 1,
  264. },
  265. },
  266. }
  267. anySimpleJSON = `{"an":{"@type":"something.example.com/jsonpb.Simple","oBool":true}}`
  268. anySimplePrettyJSON = `{
  269. "an": {
  270. "@type": "something.example.com/jsonpb.Simple",
  271. "oBool": true
  272. }
  273. }`
  274. anyWellKnown = &pb.KnownTypes{
  275. An: &anypb.Any{
  276. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  277. Value: []byte{
  278. // &durpb.Duration{Seconds: 1, Nanos: 212000000 }
  279. 1 << 3, 1, // seconds
  280. 2 << 3, 0x80, 0xba, 0x8b, 0x65, // nanos
  281. },
  282. },
  283. }
  284. anyWellKnownJSON = `{"an":{"@type":"type.googleapis.com/google.protobuf.Duration","value":"1.212s"}}`
  285. anyWellKnownPrettyJSON = `{
  286. "an": {
  287. "@type": "type.googleapis.com/google.protobuf.Duration",
  288. "value": "1.212s"
  289. }
  290. }`
  291. nonFinites = &pb.NonFinites{
  292. FNan: proto.Float32(float32(math.NaN())),
  293. FPinf: proto.Float32(float32(math.Inf(1))),
  294. FNinf: proto.Float32(float32(math.Inf(-1))),
  295. DNan: proto.Float64(float64(math.NaN())),
  296. DPinf: proto.Float64(float64(math.Inf(1))),
  297. DNinf: proto.Float64(float64(math.Inf(-1))),
  298. }
  299. nonFinitesJSON = `{` +
  300. `"fNan":"NaN",` +
  301. `"fPinf":"Infinity",` +
  302. `"fNinf":"-Infinity",` +
  303. `"dNan":"NaN",` +
  304. `"dPinf":"Infinity",` +
  305. `"dNinf":"-Infinity"` +
  306. `}`
  307. )
  308. func init() {
  309. if err := proto.SetExtension(realNumber, pb.E_Name, &realNumberName); err != nil {
  310. panic(err)
  311. }
  312. if err := proto.SetExtension(realNumber, pb.E_Complex_RealExtension, complexNumber); err != nil {
  313. panic(err)
  314. }
  315. }
  316. var marshalingTests = []struct {
  317. desc string
  318. marshaler Marshaler
  319. pb proto.Message
  320. json string
  321. }{
  322. {"simple flat object", marshaler, simpleObject, simpleObjectJSON},
  323. {"simple pretty object", marshalerAllOptions, simpleObject, simpleObjectPrettyJSON},
  324. {"non-finite floats fields object", marshaler, nonFinites, nonFinitesJSON},
  325. {"repeated fields flat object", marshaler, repeatsObject, repeatsObjectJSON},
  326. {"repeated fields pretty object", marshalerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
  327. {"nested message/enum flat object", marshaler, complexObject, complexObjectJSON},
  328. {"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
  329. {"enum-string flat object", Marshaler{},
  330. &pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
  331. {"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
  332. &pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
  333. {"unknown enum value object", marshalerAllOptions,
  334. &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},
  335. {"repeated proto3 enum", Marshaler{},
  336. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  337. proto3pb.Message_PUNS,
  338. proto3pb.Message_SLAPSTICK,
  339. }},
  340. `{"rFunny":["PUNS","SLAPSTICK"]}`},
  341. {"repeated proto3 enum as int", Marshaler{EnumsAsInts: true},
  342. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  343. proto3pb.Message_PUNS,
  344. proto3pb.Message_SLAPSTICK,
  345. }},
  346. `{"rFunny":[1,2]}`},
  347. {"empty value", marshaler, &pb.Simple3{}, `{}`},
  348. {"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
  349. {"empty repeated emitted", Marshaler{EmitDefaults: true}, &pb.SimpleSlice3{}, `{"slices":[]}`},
  350. {"empty map emitted", Marshaler{EmitDefaults: true}, &pb.SimpleMap3{}, `{"stringy":{}}`},
  351. {"nested struct null", Marshaler{EmitDefaults: true}, &pb.SimpleNull3{}, `{"simple":null}`},
  352. {"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
  353. {"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
  354. {"map<string, string>", marshaler,
  355. &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
  356. `{"strry":{"\"one\"":"two","three":"four"}}`},
  357. {"map<int32, Object>", marshaler,
  358. &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
  359. {"map<int32, Object>", marshalerAllOptions,
  360. &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, objjyPrettyJSON},
  361. {"map<int64, string>", marshaler, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
  362. `{"buggy":{"1234":"yup"}}`},
  363. {"map<bool, bool>", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
  364. // TODO: This is broken.
  365. //{"map<string, enum>", marshaler, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":"ROMAN"}`},
  366. {"map<string, enum as int>", Marshaler{EnumsAsInts: true}, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":2}}`},
  367. {"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}}`},
  368. {"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}}`},
  369. {"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}}`},
  370. {"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}}`},
  371. {"proto2 map<int64, string>", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
  372. `{"mInt64Str":{"213":"cat"}}`},
  373. {"proto2 map<bool, Object>", marshaler,
  374. &pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: {OInt32: proto.Int32(1)}}},
  375. `{"mBoolSimple":{"true":{"oInt32":1}}}`},
  376. {"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`},
  377. {"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`},
  378. {"force orig_name", Marshaler{OrigName: true}, &pb.Simple{OInt32: proto.Int32(4)},
  379. `{"o_int32":4}`},
  380. {"proto2 extension", marshaler, realNumber, realNumberJSON},
  381. {"Any with message", marshaler, anySimple, anySimpleJSON},
  382. {"Any with message and indent", marshalerAllOptions, anySimple, anySimplePrettyJSON},
  383. {"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON},
  384. {"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON},
  385. {"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3.000s"}`},
  386. {"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 100000000, Nanos: 1}}, `{"dur":"100000000.000000001s"}`},
  387. {"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{
  388. Fields: map[string]*stpb.Value{
  389. "one": {Kind: &stpb.Value_StringValue{"loneliest number"}},
  390. "two": {Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}},
  391. },
  392. }}, `{"st":{"one":"loneliest number","two":null}}`},
  393. {"empty ListValue", marshaler, &pb.KnownTypes{Lv: &stpb.ListValue{}}, `{"lv":[]}`},
  394. {"basic ListValue", marshaler, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
  395. {Kind: &stpb.Value_StringValue{"x"}},
  396. {Kind: &stpb.Value_NullValue{}},
  397. {Kind: &stpb.Value_NumberValue{3}},
  398. {Kind: &stpb.Value_BoolValue{true}},
  399. }}}, `{"lv":["x",null,3,true]}`},
  400. {"Timestamp", marshaler, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}, `{"ts":"2014-05-13T16:53:20.021Z"}`},
  401. {"number Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NumberValue{1}}}, `{"val":1}`},
  402. {"null Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}}}, `{"val":null}`},
  403. {"string number value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"9223372036854775807"}}}, `{"val":"9223372036854775807"}`},
  404. {"list of lists Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{
  405. Kind: &stpb.Value_ListValue{&stpb.ListValue{
  406. Values: []*stpb.Value{
  407. {Kind: &stpb.Value_StringValue{"x"}},
  408. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  409. Values: []*stpb.Value{
  410. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  411. Values: []*stpb.Value{{Kind: &stpb.Value_StringValue{"y"}}},
  412. }}},
  413. {Kind: &stpb.Value_StringValue{"z"}},
  414. },
  415. }}},
  416. },
  417. }},
  418. }}, `{"val":["x",[["y"],"z"]]}`},
  419. {"DoubleValue", marshaler, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}, `{"dbl":1.2}`},
  420. {"FloatValue", marshaler, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}, `{"flt":1.2}`},
  421. {"Int64Value", marshaler, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}, `{"i64":"-3"}`},
  422. {"UInt64Value", marshaler, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}, `{"u64":"3"}`},
  423. {"Int32Value", marshaler, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}, `{"i32":-4}`},
  424. {"UInt32Value", marshaler, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}, `{"u32":4}`},
  425. {"BoolValue", marshaler, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}, `{"bool":true}`},
  426. {"StringValue", marshaler, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}, `{"str":"plush"}`},
  427. {"BytesValue", marshaler, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}, `{"bytes":"d293"}`},
  428. }
  429. func TestMarshaling(t *testing.T) {
  430. for _, tt := range marshalingTests {
  431. json, err := tt.marshaler.MarshalToString(tt.pb)
  432. if err != nil {
  433. t.Errorf("%s: marshaling error: %v", tt.desc, err)
  434. } else if tt.json != json {
  435. t.Errorf("%s: got [%v] want [%v]", tt.desc, json, tt.json)
  436. }
  437. }
  438. }
  439. func TestMarshalJSONPBMarshaler(t *testing.T) {
  440. rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
  441. msg := dynamicMessage{rawJson: rawJson}
  442. str, err := new(Marshaler).MarshalToString(&msg)
  443. if err != nil {
  444. t.Errorf("an unexpected error occurred when marshalling JSONPBMarshaler: %v", err)
  445. }
  446. if str != rawJson {
  447. t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, rawJson)
  448. }
  449. }
  450. func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
  451. msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
  452. a, err := ptypes.MarshalAny(&msg)
  453. if err != nil {
  454. t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
  455. }
  456. str, err := new(Marshaler).MarshalToString(a)
  457. if err != nil {
  458. t.Errorf("an unexpected error occurred when marshalling Any to JSON: %v", err)
  459. }
  460. // after custom marshaling, it's round-tripped through JSON decoding/encoding already,
  461. // so the keys are sorted, whitespace is compacted, and "@type" key has been added
  462. expected := `{"@type":"type.googleapis.com/` + dynamicMessageName + `","baz":[0,1,2,3],"foo":"bar"}`
  463. if str != expected {
  464. t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, expected)
  465. }
  466. }
  467. var unmarshalingTests = []struct {
  468. desc string
  469. unmarshaler Unmarshaler
  470. json string
  471. pb proto.Message
  472. }{
  473. {"simple flat object", Unmarshaler{}, simpleObjectJSON, simpleObject},
  474. {"simple pretty object", Unmarshaler{}, simpleObjectPrettyJSON, simpleObject},
  475. {"repeated fields flat object", Unmarshaler{}, repeatsObjectJSON, repeatsObject},
  476. {"repeated fields pretty object", Unmarshaler{}, repeatsObjectPrettyJSON, repeatsObject},
  477. {"nested message/enum flat object", Unmarshaler{}, complexObjectJSON, complexObject},
  478. {"nested message/enum pretty object", Unmarshaler{}, complexObjectPrettyJSON, complexObject},
  479. {"enum-string object", Unmarshaler{}, `{"color":"BLUE"}`, &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
  480. {"enum-value object", Unmarshaler{}, "{\n \"color\": 2\n}", &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
  481. {"unknown field with allowed option", Unmarshaler{AllowUnknownFields: true}, `{"unknown": "foo"}`, new(pb.Simple)},
  482. {"proto3 enum string", Unmarshaler{}, `{"hilarity":"PUNS"}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  483. {"proto3 enum value", Unmarshaler{}, `{"hilarity":1}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  484. {"unknown enum value object",
  485. Unmarshaler{},
  486. "{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}",
  487. &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}},
  488. {"repeated proto3 enum", Unmarshaler{}, `{"rFunny":["PUNS","SLAPSTICK"]}`,
  489. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  490. proto3pb.Message_PUNS,
  491. proto3pb.Message_SLAPSTICK,
  492. }}},
  493. {"repeated proto3 enum as int", Unmarshaler{}, `{"rFunny":[1,2]}`,
  494. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  495. proto3pb.Message_PUNS,
  496. proto3pb.Message_SLAPSTICK,
  497. }}},
  498. {"repeated proto3 enum as mix of strings and ints", Unmarshaler{}, `{"rFunny":["PUNS",2]}`,
  499. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  500. proto3pb.Message_PUNS,
  501. proto3pb.Message_SLAPSTICK,
  502. }}},
  503. {"unquoted int64 object", Unmarshaler{}, `{"oInt64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}},
  504. {"unquoted uint64 object", Unmarshaler{}, `{"oUint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}},
  505. {"NaN", Unmarshaler{}, `{"oDouble":"NaN"}`, &pb.Simple{ODouble: proto.Float64(math.NaN())}},
  506. {"Inf", Unmarshaler{}, `{"oFloat":"Infinity"}`, &pb.Simple{OFloat: proto.Float32(float32(math.Inf(1)))}},
  507. {"-Inf", Unmarshaler{}, `{"oDouble":"-Infinity"}`, &pb.Simple{ODouble: proto.Float64(math.Inf(-1))}},
  508. {"map<int64, int32>", Unmarshaler{}, `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
  509. {"map<string, string>", Unmarshaler{}, `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
  510. {"map<int32, Object>", Unmarshaler{}, `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}},
  511. {"proto2 extension", Unmarshaler{}, realNumberJSON, realNumber},
  512. {"Any with message", Unmarshaler{}, anySimpleJSON, anySimple},
  513. {"Any with message and indent", Unmarshaler{}, anySimplePrettyJSON, anySimple},
  514. {"Any with WKT", Unmarshaler{}, anyWellKnownJSON, anyWellKnown},
  515. {"Any with WKT and indent", Unmarshaler{}, anyWellKnownPrettyJSON, anyWellKnown},
  516. // TODO: This is broken.
  517. //{"map<string, enum>", Unmarshaler{}, `{"enumy":{"XIV":"ROMAN"}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
  518. {"map<string, enum as int>", Unmarshaler{}, `{"enumy":{"XIV":2}}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
  519. {"oneof", Unmarshaler{}, `{"salary":31000}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Salary{31000}}},
  520. {"oneof spec name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
  521. {"oneof orig_name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
  522. {"oneof spec name2", Unmarshaler{}, `{"homeAddress":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
  523. {"oneof orig_name2", Unmarshaler{}, `{"home_address":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
  524. {"orig_name input", Unmarshaler{}, `{"o_bool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
  525. {"camelName input", Unmarshaler{}, `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
  526. {"Duration", Unmarshaler{}, `{"dur":"3.000s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
  527. {"null Duration", Unmarshaler{}, `{"dur":null}`, &pb.KnownTypes{Dur: nil}},
  528. {"Timestamp", Unmarshaler{}, `{"ts":"2014-05-13T16:53:20.021Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}},
  529. {"PreEpochTimestamp", Unmarshaler{}, `{"ts":"1969-12-31T23:59:58.999999995Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -2, Nanos: 999999995}}},
  530. {"ZeroTimeTimestamp", Unmarshaler{}, `{"ts":"0001-01-01T00:00:00Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -62135596800, Nanos: 0}}},
  531. {"null Timestamp", Unmarshaler{}, `{"ts":null}`, &pb.KnownTypes{Ts: nil}},
  532. {"null Struct", Unmarshaler{}, `{"st": null}`, &pb.KnownTypes{St: nil}},
  533. {"empty Struct", Unmarshaler{}, `{"st": {}}`, &pb.KnownTypes{St: &stpb.Struct{}}},
  534. {"basic Struct", Unmarshaler{}, `{"st": {"a": "x", "b": null, "c": 3, "d": true}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
  535. "a": {Kind: &stpb.Value_StringValue{"x"}},
  536. "b": {Kind: &stpb.Value_NullValue{}},
  537. "c": {Kind: &stpb.Value_NumberValue{3}},
  538. "d": {Kind: &stpb.Value_BoolValue{true}},
  539. }}}},
  540. {"nested Struct", Unmarshaler{}, `{"st": {"a": {"b": 1, "c": [{"d": true}, "f"]}}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
  541. "a": {Kind: &stpb.Value_StructValue{&stpb.Struct{Fields: map[string]*stpb.Value{
  542. "b": {Kind: &stpb.Value_NumberValue{1}},
  543. "c": {Kind: &stpb.Value_ListValue{&stpb.ListValue{Values: []*stpb.Value{
  544. {Kind: &stpb.Value_StructValue{&stpb.Struct{Fields: map[string]*stpb.Value{"d": {Kind: &stpb.Value_BoolValue{true}}}}}},
  545. {Kind: &stpb.Value_StringValue{"f"}},
  546. }}}},
  547. }}}},
  548. }}}},
  549. {"null ListValue", Unmarshaler{}, `{"lv": null}`, &pb.KnownTypes{Lv: nil}},
  550. {"empty ListValue", Unmarshaler{}, `{"lv": []}`, &pb.KnownTypes{Lv: &stpb.ListValue{}}},
  551. {"basic ListValue", Unmarshaler{}, `{"lv": ["x", null, 3, true]}`, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
  552. {Kind: &stpb.Value_StringValue{"x"}},
  553. {Kind: &stpb.Value_NullValue{}},
  554. {Kind: &stpb.Value_NumberValue{3}},
  555. {Kind: &stpb.Value_BoolValue{true}},
  556. }}}},
  557. {"number Value", Unmarshaler{}, `{"val":1}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NumberValue{1}}}},
  558. {"null Value", Unmarshaler{}, `{"val":null}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}}}},
  559. {"bool Value", Unmarshaler{}, `{"val":true}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_BoolValue{true}}}},
  560. {"string Value", Unmarshaler{}, `{"val":"x"}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"x"}}}},
  561. {"string number value", Unmarshaler{}, `{"val":"9223372036854775807"}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"9223372036854775807"}}}},
  562. {"list of lists Value", Unmarshaler{}, `{"val":["x", [["y"], "z"]]}`, &pb.KnownTypes{Val: &stpb.Value{
  563. Kind: &stpb.Value_ListValue{&stpb.ListValue{
  564. Values: []*stpb.Value{
  565. {Kind: &stpb.Value_StringValue{"x"}},
  566. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  567. Values: []*stpb.Value{
  568. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  569. Values: []*stpb.Value{{Kind: &stpb.Value_StringValue{"y"}}},
  570. }}},
  571. {Kind: &stpb.Value_StringValue{"z"}},
  572. },
  573. }}},
  574. },
  575. }}}}},
  576. {"DoubleValue", Unmarshaler{}, `{"dbl":1.2}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}},
  577. {"FloatValue", Unmarshaler{}, `{"flt":1.2}`, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}},
  578. {"Int64Value", Unmarshaler{}, `{"i64":"-3"}`, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}},
  579. {"UInt64Value", Unmarshaler{}, `{"u64":"3"}`, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}},
  580. {"Int32Value", Unmarshaler{}, `{"i32":-4}`, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}},
  581. {"UInt32Value", Unmarshaler{}, `{"u32":4}`, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}},
  582. {"BoolValue", Unmarshaler{}, `{"bool":true}`, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}},
  583. {"StringValue", Unmarshaler{}, `{"str":"plush"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}},
  584. {"BytesValue", Unmarshaler{}, `{"bytes":"d293"}`, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}},
  585. // Ensure that `null` as a value ends up with a nil pointer instead of a [type]Value struct.
  586. {"null DoubleValue", Unmarshaler{}, `{"dbl":null}`, &pb.KnownTypes{Dbl: nil}},
  587. {"null FloatValue", Unmarshaler{}, `{"flt":null}`, &pb.KnownTypes{Flt: nil}},
  588. {"null Int64Value", Unmarshaler{}, `{"i64":null}`, &pb.KnownTypes{I64: nil}},
  589. {"null UInt64Value", Unmarshaler{}, `{"u64":null}`, &pb.KnownTypes{U64: nil}},
  590. {"null Int32Value", Unmarshaler{}, `{"i32":null}`, &pb.KnownTypes{I32: nil}},
  591. {"null UInt32Value", Unmarshaler{}, `{"u32":null}`, &pb.KnownTypes{U32: nil}},
  592. {"null BoolValue", Unmarshaler{}, `{"bool":null}`, &pb.KnownTypes{Bool: nil}},
  593. {"null StringValue", Unmarshaler{}, `{"str":null}`, &pb.KnownTypes{Str: nil}},
  594. {"null BytesValue", Unmarshaler{}, `{"bytes":null}`, &pb.KnownTypes{Bytes: nil}},
  595. }
  596. func TestUnmarshaling(t *testing.T) {
  597. for _, tt := range unmarshalingTests {
  598. // Make a new instance of the type of our expected object.
  599. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
  600. err := tt.unmarshaler.Unmarshal(strings.NewReader(tt.json), p)
  601. if err != nil {
  602. t.Errorf("%s: %v", tt.desc, err)
  603. continue
  604. }
  605. // For easier diffs, compare text strings of the protos.
  606. exp := proto.MarshalTextString(tt.pb)
  607. act := proto.MarshalTextString(p)
  608. if string(exp) != string(act) {
  609. t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
  610. }
  611. }
  612. }
  613. func TestUnmarshalNullArray(t *testing.T) {
  614. var repeats pb.Repeats
  615. if err := UnmarshalString(`{"rBool":null}`, &repeats); err != nil {
  616. t.Fatal(err)
  617. }
  618. if !reflect.DeepEqual(repeats, pb.Repeats{}) {
  619. t.Errorf("got non-nil fields in [%#v]", repeats)
  620. }
  621. }
  622. func TestUnmarshalNullObject(t *testing.T) {
  623. var maps pb.Maps
  624. if err := UnmarshalString(`{"mInt64Str":null}`, &maps); err != nil {
  625. t.Fatal(err)
  626. }
  627. if !reflect.DeepEqual(maps, pb.Maps{}) {
  628. t.Errorf("got non-nil fields in [%#v]", maps)
  629. }
  630. }
  631. func TestUnmarshalNext(t *testing.T) {
  632. // We only need to check against a few, not all of them.
  633. tests := unmarshalingTests[:5]
  634. // Create a buffer with many concatenated JSON objects.
  635. var b bytes.Buffer
  636. for _, tt := range tests {
  637. b.WriteString(tt.json)
  638. }
  639. dec := json.NewDecoder(&b)
  640. for _, tt := range tests {
  641. // Make a new instance of the type of our expected object.
  642. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
  643. err := tt.unmarshaler.UnmarshalNext(dec, p)
  644. if err != nil {
  645. t.Errorf("%s: %v", tt.desc, err)
  646. continue
  647. }
  648. // For easier diffs, compare text strings of the protos.
  649. exp := proto.MarshalTextString(tt.pb)
  650. act := proto.MarshalTextString(p)
  651. if string(exp) != string(act) {
  652. t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
  653. }
  654. }
  655. p := &pb.Simple{}
  656. err := new(Unmarshaler).UnmarshalNext(dec, p)
  657. if err != io.EOF {
  658. t.Errorf("eof: got %v, expected io.EOF", err)
  659. }
  660. }
  661. var unmarshalingShouldError = []struct {
  662. desc string
  663. in string
  664. pb proto.Message
  665. }{
  666. {"a value", "666", new(pb.Simple)},
  667. {"gibberish", "{adskja123;l23=-=", new(pb.Simple)},
  668. {"unknown field", `{"unknown": "foo"}`, new(pb.Simple)},
  669. {"unknown enum name", `{"hilarity":"DAVE"}`, new(proto3pb.Message)},
  670. }
  671. func TestUnmarshalingBadInput(t *testing.T) {
  672. for _, tt := range unmarshalingShouldError {
  673. err := UnmarshalString(tt.in, tt.pb)
  674. if err == nil {
  675. t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
  676. }
  677. }
  678. }
  679. type funcResolver func(turl string) (proto.Message, error)
  680. func (fn funcResolver) Resolve(turl string) (proto.Message, error) {
  681. return fn(turl)
  682. }
  683. func TestAnyWithCustomResolver(t *testing.T) {
  684. var resolvedTypeUrls []string
  685. resolver := funcResolver(func(turl string) (proto.Message, error) {
  686. resolvedTypeUrls = append(resolvedTypeUrls, turl)
  687. return new(pb.Simple), nil
  688. })
  689. msg := &pb.Simple{
  690. OBytes: []byte{1, 2, 3, 4},
  691. OBool: proto.Bool(true),
  692. OString: proto.String("foobar"),
  693. OInt64: proto.Int64(1020304),
  694. }
  695. msgBytes, err := proto.Marshal(msg)
  696. if err != nil {
  697. t.Errorf("an unexpected error occurred when marshaling message: %v", err)
  698. }
  699. // make an Any with a type URL that won't resolve w/out custom resolver
  700. any := &anypb.Any{
  701. TypeUrl: "https://foobar.com/some.random.MessageKind",
  702. Value: msgBytes,
  703. }
  704. m := Marshaler{AnyResolver: resolver}
  705. js, err := m.MarshalToString(any)
  706. if err != nil {
  707. t.Errorf("an unexpected error occurred when marshaling any to JSON: %v", err)
  708. }
  709. if len(resolvedTypeUrls) != 1 {
  710. t.Errorf("custom resolver was not invoked during marshaling")
  711. } else if resolvedTypeUrls[0] != "https://foobar.com/some.random.MessageKind" {
  712. t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[0], "https://foobar.com/some.random.MessageKind")
  713. }
  714. wanted := `{"@type":"https://foobar.com/some.random.MessageKind","oBool":true,"oInt64":"1020304","oString":"foobar","oBytes":"AQIDBA=="}`
  715. if js != wanted {
  716. t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", js, wanted)
  717. }
  718. u := Unmarshaler{AnyResolver: resolver}
  719. roundTrip := &anypb.Any{}
  720. err = u.Unmarshal(bytes.NewReader([]byte(js)), roundTrip)
  721. if err != nil {
  722. t.Errorf("an unexpected error occurred when unmarshaling any from JSON: %v", err)
  723. }
  724. if len(resolvedTypeUrls) != 2 {
  725. t.Errorf("custom resolver was not invoked during marshaling")
  726. } else if resolvedTypeUrls[1] != "https://foobar.com/some.random.MessageKind" {
  727. t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[1], "https://foobar.com/some.random.MessageKind")
  728. }
  729. if !proto.Equal(any, roundTrip) {
  730. t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", roundTrip, any)
  731. }
  732. }
  733. func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
  734. rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
  735. var msg dynamicMessage
  736. if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
  737. t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
  738. }
  739. if msg.rawJson != rawJson {
  740. t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.rawJson, rawJson)
  741. }
  742. }
  743. func TestUnmarshalNullWithJSONPBUnmarshaler(t *testing.T) {
  744. rawJson := `{"stringField":null}`
  745. var ptrFieldMsg ptrFieldMessage
  746. if err := Unmarshal(strings.NewReader(rawJson), &ptrFieldMsg); err != nil {
  747. t.Errorf("unmarshal error: %v", err)
  748. }
  749. want := ptrFieldMessage{StringField: &stringField{IsSet: true, StringValue: "null"}}
  750. if !proto.Equal(&ptrFieldMsg, &want) {
  751. t.Errorf("unmarshal result StringField: got %v, want %v", ptrFieldMsg, want)
  752. }
  753. }
  754. func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
  755. rawJson := `{ "@type": "blah.com/` + dynamicMessageName + `", "foo": "bar", "baz": [0, 1, 2, 3] }`
  756. var got anypb.Any
  757. if err := Unmarshal(strings.NewReader(rawJson), &got); err != nil {
  758. t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
  759. }
  760. dm := &dynamicMessage{rawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
  761. var want anypb.Any
  762. if b, err := proto.Marshal(dm); err != nil {
  763. t.Errorf("an unexpected error occurred when marshaling message: %v", err)
  764. } else {
  765. want.TypeUrl = "blah.com/" + dynamicMessageName
  766. want.Value = b
  767. }
  768. if !proto.Equal(&got, &want) {
  769. t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", got, want)
  770. }
  771. }
  772. const (
  773. dynamicMessageName = "google.protobuf.jsonpb.testing.dynamicMessage"
  774. )
  775. func init() {
  776. // we register the custom type below so that we can use it in Any types
  777. proto.RegisterType((*dynamicMessage)(nil), dynamicMessageName)
  778. }
  779. type ptrFieldMessage struct {
  780. StringField *stringField `protobuf:"bytes,1,opt,name=stringField"`
  781. }
  782. func (m *ptrFieldMessage) Reset() {
  783. }
  784. func (m *ptrFieldMessage) String() string {
  785. return m.StringField.StringValue
  786. }
  787. func (m *ptrFieldMessage) ProtoMessage() {
  788. }
  789. type stringField struct {
  790. IsSet bool `protobuf:"varint,1,opt,name=isSet"`
  791. StringValue string `protobuf:"bytes,2,opt,name=stringValue"`
  792. }
  793. func (s *stringField) Reset() {
  794. }
  795. func (s *stringField) String() string {
  796. return s.StringValue
  797. }
  798. func (s *stringField) ProtoMessage() {
  799. }
  800. func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
  801. s.IsSet = true
  802. s.StringValue = string(js)
  803. return nil
  804. }
  805. // dynamicMessage implements protobuf.Message but is not a normal generated message type.
  806. // It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
  807. type dynamicMessage struct {
  808. rawJson string `protobuf:"bytes,1,opt,name=rawJson"`
  809. }
  810. func (m *dynamicMessage) Reset() {
  811. m.rawJson = "{}"
  812. }
  813. func (m *dynamicMessage) String() string {
  814. return m.rawJson
  815. }
  816. func (m *dynamicMessage) ProtoMessage() {
  817. }
  818. func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
  819. return []byte(m.rawJson), nil
  820. }
  821. func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
  822. m.rawJson = string(js)
  823. return nil
  824. }