blob: 42455043ee082e83293769eda133af2d47316fb9 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// https://developers.google.com/protocol-buffers/
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#endregion
32
33using System;
34using Google.Protobuf.TestProtos;
35using NUnit.Framework;
36using UnitTest.Issues.TestProtos;
37using Google.Protobuf.WellKnownTypes;
38using Google.Protobuf.Reflection;
39
40using static Google.Protobuf.JsonParserTest; // For WrapInQuotes
41
42namespace Google.Protobuf
43{
44 /// <summary>
45 /// Tests for the JSON formatter. Note that in these tests, double quotes are replaced with apostrophes
46 /// for the sake of readability (embedding \" everywhere is painful). See the AssertJson method for details.
47 /// </summary>
48 public class JsonFormatterTest
49 {
50 [Test]
51 public void DefaultValues_WhenOmitted()
52 {
53 var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));
54
55 AssertJson("{ }", formatter.Format(new ForeignMessage()));
56 AssertJson("{ }", formatter.Format(new TestAllTypes()));
57 AssertJson("{ }", formatter.Format(new TestMap()));
58 }
59
60 [Test]
61 public void DefaultValues_WhenIncluded()
62 {
63 var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
64 AssertJson("{ 'c': 0 }", formatter.Format(new ForeignMessage()));
65 }
66
67 [Test]
68 public void AllSingleFields()
69 {
70 var message = new TestAllTypes
71 {
72 SingleBool = true,
73 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
74 SingleDouble = 23.5,
75 SingleFixed32 = 23,
76 SingleFixed64 = 1234567890123,
77 SingleFloat = 12.25f,
78 SingleForeignEnum = ForeignEnum.FOREIGN_BAR,
79 SingleForeignMessage = new ForeignMessage { C = 10 },
80 SingleImportEnum = ImportEnum.IMPORT_BAZ,
81 SingleImportMessage = new ImportMessage { D = 20 },
82 SingleInt32 = 100,
83 SingleInt64 = 3210987654321,
84 SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO,
85 SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
86 SinglePublicImportMessage = new PublicImportMessage { E = 54 },
87 SingleSfixed32 = -123,
88 SingleSfixed64 = -12345678901234,
89 SingleSint32 = -456,
90 SingleSint64 = -12345678901235,
91 SingleString = "test\twith\ttabs",
92 SingleUint32 = uint.MaxValue,
93 SingleUint64 = ulong.MaxValue,
94 };
95 var actualText = JsonFormatter.Default.Format(message);
96
97 // Fields in numeric order
98 var expectedText = "{ " +
99 "'singleInt32': 100, " +
100 "'singleInt64': '3210987654321', " +
101 "'singleUint32': 4294967295, " +
102 "'singleUint64': '18446744073709551615', " +
103 "'singleSint32': -456, " +
104 "'singleSint64': '-12345678901235', " +
105 "'singleFixed32': 23, " +
106 "'singleFixed64': '1234567890123', " +
107 "'singleSfixed32': -123, " +
108 "'singleSfixed64': '-12345678901234', " +
109 "'singleFloat': 12.25, " +
110 "'singleDouble': 23.5, " +
111 "'singleBool': true, " +
112 "'singleString': 'test\\twith\\ttabs', " +
113 "'singleBytes': 'AQIDBA==', " +
114 "'singleNestedMessage': { 'bb': 35 }, " +
115 "'singleForeignMessage': { 'c': 10 }, " +
116 "'singleImportMessage': { 'd': 20 }, " +
117 "'singleNestedEnum': 'FOO', " +
118 "'singleForeignEnum': 'FOREIGN_BAR', " +
119 "'singleImportEnum': 'IMPORT_BAZ', " +
120 "'singlePublicImportMessage': { 'e': 54 }" +
121 " }";
122 AssertJson(expectedText, actualText);
123 }
124
125 [Test]
126 public void RepeatedField()
127 {
128 AssertJson("{ 'repeatedInt32': [ 1, 2, 3, 4, 5 ] }",
129 JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
130 }
131
132 [Test]
133 public void MapField_StringString()
134 {
135 AssertJson("{ 'mapStringString': { 'with spaces': 'bar', 'a': 'b' } }",
136 JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
137 }
138
139 [Test]
140 public void MapField_Int32Int32()
141 {
142 // The keys are quoted, but the values aren't.
143 AssertJson("{ 'mapInt32Int32': { '0': 1, '2': 3 } }",
144 JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
145 }
146
147 [Test]
148 public void MapField_BoolBool()
149 {
150 // The keys are quoted, but the values aren't.
151 AssertJson("{ 'mapBoolBool': { 'false': true, 'true': false } }",
152 JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
153 }
154
155 [TestCase(1.0, "1")]
156 [TestCase(double.NaN, "'NaN'")]
157 [TestCase(double.PositiveInfinity, "'Infinity'")]
158 [TestCase(double.NegativeInfinity, "'-Infinity'")]
159 public void DoubleRepresentations(double value, string expectedValueText)
160 {
161 var message = new TestAllTypes { SingleDouble = value };
162 string actualText = JsonFormatter.Default.Format(message);
163 string expectedText = "{ 'singleDouble': " + expectedValueText + " }";
164 AssertJson(expectedText, actualText);
165 }
166
167 [Test]
168 public void UnknownEnumValueNumeric_SingleField()
169 {
170 var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
171 AssertJson("{ 'singleForeignEnum': 100 }", JsonFormatter.Default.Format(message));
172 }
173
174 [Test]
175 public void UnknownEnumValueNumeric_RepeatedField()
176 {
177 var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } };
178 AssertJson("{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 100, 'FOREIGN_FOO' ] }", JsonFormatter.Default.Format(message));
179 }
180
181 [Test]
182 public void UnknownEnumValueNumeric_MapField()
183 {
184 var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } };
185 AssertJson("{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '2': 100, '3': 'MAP_ENUM_BAR' } }", JsonFormatter.Default.Format(message));
186 }
187
188 [Test]
189 public void UnknownEnumValue_RepeatedField_AllEntriesUnknown()
190 {
191 var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
192 AssertJson("{ 'repeatedForeignEnum': [ 200, 100 ] }", JsonFormatter.Default.Format(message));
193 }
194
195 [Test]
196 [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit
197 [TestCase("a\u0601b", "a\\u0601b")] // Ranged
198 [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...)
199 public void SimpleNonAscii(string text, string encoded)
200 {
201 var message = new TestAllTypes { SingleString = text };
202 AssertJson("{ 'singleString': '" + encoded + "' }", JsonFormatter.Default.Format(message));
203 }
204
205 [Test]
206 public void SurrogatePairEscaping()
207 {
208 var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" };
209 AssertJson("{ 'singleString': 'a\\ud801\\udc01b' }", JsonFormatter.Default.Format(message));
210 }
211
212 [Test]
213 public void InvalidSurrogatePairsFail()
214 {
215 // Note: don't use TestCase for these, as the strings can't be reliably represented
216 // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/
217
218 // Lone low surrogate
219 var message = new TestAllTypes { SingleString = "a\uDC01b" };
220 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
221
222 // Lone high surrogate
223 message = new TestAllTypes { SingleString = "a\uD801b" };
224 Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
225 }
226
227 [Test]
228 [TestCase("foo_bar", "fooBar")]
229 [TestCase("bananaBanana", "bananaBanana")]
230 [TestCase("BANANABanana", "bananaBanana")]
231 public void ToCamelCase(string original, string expected)
232 {
233 Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original));
234 }
235
236 [Test]
237 [TestCase(null, "{ }")]
238 [TestCase("x", "{ 'fooString': 'x' }")]
239 [TestCase("", "{ 'fooString': '' }")]
240 public void Oneof(string fooStringValue, string expectedJson)
241 {
242 var message = new TestOneof();
243 if (fooStringValue != null)
244 {
245 message.FooString = fooStringValue;
246 }
247
248 // We should get the same result both with and without "format default values".
249 var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
250 AssertJson(expectedJson, formatter.Format(message));
251 formatter = new JsonFormatter(new JsonFormatter.Settings(true));
252 AssertJson(expectedJson, formatter.Format(message));
253 }
254
255 [Test]
256 public void WrapperFormatting_Single()
257 {
258 // Just a few examples, handling both classes and value types, and
259 // default vs non-default values
260 var message = new TestWellKnownTypes
261 {
262 Int64Field = 10,
263 Int32Field = 0,
264 BytesField = ByteString.FromBase64("ABCD"),
265 StringField = ""
266 };
267 var expectedJson = "{ 'int64Field': '10', 'int32Field': 0, 'stringField': '', 'bytesField': 'ABCD' }";
268 AssertJson(expectedJson, JsonFormatter.Default.Format(message));
269 }
270
271 [Test]
272 public void WrapperFormatting_Message()
273 {
274 Assert.AreEqual("\"\"", JsonFormatter.Default.Format(new StringValue()));
275 Assert.AreEqual("0", JsonFormatter.Default.Format(new Int32Value()));
276 }
277
278 [Test]
279 public void WrapperFormatting_IncludeNull()
280 {
281 // The actual JSON here is very large because there are lots of fields. Just test a couple of them.
282 var message = new TestWellKnownTypes { Int32Field = 10 };
283 var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
284 var actualJson = formatter.Format(message);
285 Assert.IsTrue(actualJson.Contains("\"int64Field\": null"));
286 Assert.IsFalse(actualJson.Contains("\"int32Field\": null"));
287 }
288
289 [Test]
290 public void OutputIsInNumericFieldOrder_NoDefaults()
291 {
292 var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
293 var message = new TestJsonFieldOrdering { PlainString = "p1", PlainInt32 = 2 };
294 AssertJson("{ 'plainString': 'p1', 'plainInt32': 2 }", formatter.Format(message));
295 message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
296 AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
297 message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
298 AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
299 }
300
301 [Test]
302 public void OutputIsInNumericFieldOrder_WithDefaults()
303 {
304 var formatter = new JsonFormatter(new JsonFormatter.Settings(true));
305 var message = new TestJsonFieldOrdering();
306 AssertJson("{ 'plainString': '', 'plainInt32': 0 }", formatter.Format(message));
307 message = new TestJsonFieldOrdering { O1Int32 = 5, O2String = "o2", PlainInt32 = 10, PlainString = "plain" };
308 AssertJson("{ 'plainString': 'plain', 'o2String': 'o2', 'plainInt32': 10, 'o1Int32': 5 }", formatter.Format(message));
309 message = new TestJsonFieldOrdering { O1String = "", O2Int32 = 0, PlainInt32 = 10, PlainString = "plain" };
310 AssertJson("{ 'plainString': 'plain', 'o1String': '', 'plainInt32': 10, 'o2Int32': 0 }", formatter.Format(message));
311 }
312
313 [Test]
314 [TestCase("1970-01-01T00:00:00Z", 0)]
315 [TestCase("1970-01-01T00:00:00.000000001Z", 1)]
316 [TestCase("1970-01-01T00:00:00.000000010Z", 10)]
317 [TestCase("1970-01-01T00:00:00.000000100Z", 100)]
318 [TestCase("1970-01-01T00:00:00.000001Z", 1000)]
319 [TestCase("1970-01-01T00:00:00.000010Z", 10000)]
320 [TestCase("1970-01-01T00:00:00.000100Z", 100000)]
321 [TestCase("1970-01-01T00:00:00.001Z", 1000000)]
322 [TestCase("1970-01-01T00:00:00.010Z", 10000000)]
323 [TestCase("1970-01-01T00:00:00.100Z", 100000000)]
324 [TestCase("1970-01-01T00:00:00.100Z", 100000000)]
325 [TestCase("1970-01-01T00:00:00.120Z", 120000000)]
326 [TestCase("1970-01-01T00:00:00.123Z", 123000000)]
327 [TestCase("1970-01-01T00:00:00.123400Z", 123400000)]
328 [TestCase("1970-01-01T00:00:00.123450Z", 123450000)]
329 [TestCase("1970-01-01T00:00:00.123456Z", 123456000)]
330 [TestCase("1970-01-01T00:00:00.123456700Z", 123456700)]
331 [TestCase("1970-01-01T00:00:00.123456780Z", 123456780)]
332 [TestCase("1970-01-01T00:00:00.123456789Z", 123456789)]
333 public void TimestampStandalone(string expected, int nanos)
334 {
335 Assert.AreEqual(WrapInQuotes(expected), new Timestamp { Nanos = nanos }.ToString());
336 }
337
338 [Test]
339 public void TimestampStandalone_FromDateTime()
340 {
341 // One before and one after the Unix epoch, more easily represented via DateTime.
342 Assert.AreEqual("\"1673-06-19T12:34:56Z\"",
343 new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp().ToString());
344 Assert.AreEqual("\"2015-07-31T10:29:34Z\"",
345 new DateTime(2015, 7, 31, 10, 29, 34, DateTimeKind.Utc).ToTimestamp().ToString());
346 }
347
348 [Test]
349 [TestCase(-1, -1)] // Would be valid as duration
350 [TestCase(1, Timestamp.MaxNanos + 1)]
351 [TestCase(Timestamp.UnixSecondsAtBclMaxValue + 1, 0)]
352 [TestCase(Timestamp.UnixSecondsAtBclMinValue - 1, 0)]
353 public void TimestampStandalone_NonNormalized(long seconds, int nanoseconds)
354 {
355 var timestamp = new Timestamp { Seconds = seconds, Nanos = nanoseconds };
356 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(timestamp));
357 }
358
359 [Test]
360 public void TimestampField()
361 {
362 var message = new TestWellKnownTypes { TimestampField = new Timestamp() };
363 AssertJson("{ 'timestampField': '1970-01-01T00:00:00Z' }", JsonFormatter.Default.Format(message));
364 }
365
366 [Test]
367 [TestCase(0, 0, "0s")]
368 [TestCase(1, 0, "1s")]
369 [TestCase(-1, 0, "-1s")]
370 [TestCase(0, 1, "0.000000001s")]
371 [TestCase(0, 10, "0.000000010s")]
372 [TestCase(0, 100, "0.000000100s")]
373 [TestCase(0, 1000, "0.000001s")]
374 [TestCase(0, 10000, "0.000010s")]
375 [TestCase(0, 100000, "0.000100s")]
376 [TestCase(0, 1000000, "0.001s")]
377 [TestCase(0, 10000000, "0.010s")]
378 [TestCase(0, 100000000, "0.100s")]
379 [TestCase(0, 120000000, "0.120s")]
380 [TestCase(0, 123000000, "0.123s")]
381 [TestCase(0, 123400000, "0.123400s")]
382 [TestCase(0, 123450000, "0.123450s")]
383 [TestCase(0, 123456000, "0.123456s")]
384 [TestCase(0, 123456700, "0.123456700s")]
385 [TestCase(0, 123456780, "0.123456780s")]
386 [TestCase(0, 123456789, "0.123456789s")]
387 [TestCase(0, -100000000, "-0.100s")]
388 [TestCase(1, 100000000, "1.100s")]
389 [TestCase(-1, -100000000, "-1.100s")]
390 public void DurationStandalone(long seconds, int nanoseconds, string expected)
391 {
392 var json = JsonFormatter.Default.Format(new Duration { Seconds = seconds, Nanos = nanoseconds });
393 Assert.AreEqual(WrapInQuotes(expected), json);
394 }
395
396 [Test]
397 [TestCase(1, 2123456789)]
398 [TestCase(1, -100000000)]
399 public void DurationStandalone_NonNormalized(long seconds, int nanoseconds)
400 {
401 var duration = new Duration { Seconds = seconds, Nanos = nanoseconds };
402 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(duration));
403 }
404
405 [Test]
406 public void DurationField()
407 {
408 var message = new TestWellKnownTypes { DurationField = new Duration() };
409 AssertJson("{ 'durationField': '0s' }", JsonFormatter.Default.Format(message));
410 }
411
412 [Test]
413 public void StructSample()
414 {
415 var message = new Struct
416 {
417 Fields =
418 {
419 { "a", Value.ForNull() },
420 { "b", Value.ForBool(false) },
421 { "c", Value.ForNumber(10.5) },
422 { "d", Value.ForString("text") },
423 { "e", Value.ForList(Value.ForString("t1"), Value.ForNumber(5)) },
424 { "f", Value.ForStruct(new Struct { Fields = { { "nested", Value.ForString("value") } } }) }
425 }
426 };
427 AssertJson("{ 'a': null, 'b': false, 'c': 10.5, 'd': 'text', 'e': [ 't1', 5 ], 'f': { 'nested': 'value' } }", message.ToString());
428 }
429
430 [Test]
431 [TestCase("foo__bar")]
432 [TestCase("foo_3_ar")]
433 [TestCase("fooBar")]
434 public void FieldMaskInvalid(string input)
435 {
436 var mask = new FieldMask { Paths = { input } };
437 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(mask));
438 }
439
440 [Test]
441 public void FieldMaskStandalone()
442 {
443 var fieldMask = new FieldMask { Paths = { "", "single", "with_underscore", "nested.field.name", "nested..double_dot" } };
444 Assert.AreEqual("\",single,withUnderscore,nested.field.name,nested..doubleDot\"", fieldMask.ToString());
445
446 // Invalid, but we shouldn't create broken JSON...
447 fieldMask = new FieldMask { Paths = { "x\\y" } };
448 Assert.AreEqual(@"""x\\y""", fieldMask.ToString());
449 }
450
451 [Test]
452 public void FieldMaskField()
453 {
454 var message = new TestWellKnownTypes { FieldMaskField = new FieldMask { Paths = { "user.display_name", "photo" } } };
455 AssertJson("{ 'fieldMaskField': 'user.displayName,photo' }", JsonFormatter.Default.Format(message));
456 }
457
458 // SourceContext is an example of a well-known type with no special JSON handling
459 [Test]
460 public void SourceContextStandalone()
461 {
462 var message = new SourceContext { FileName = "foo.proto" };
463 AssertJson("{ 'fileName': 'foo.proto' }", JsonFormatter.Default.Format(message));
464 }
465
466 [Test]
467 public void AnyWellKnownType()
468 {
469 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(Timestamp.Descriptor)));
470 var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
471 var any = Any.Pack(timestamp);
472 AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any));
473 }
474
475 [Test]
476 public void AnyMessageType()
477 {
478 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
479 var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
480 var any = Any.Pack(message);
481 AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any));
482 }
483
484 [Test]
485 public void AnyNested()
486 {
487 var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
488 var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));
489
490 // Nest an Any as the value of an Any.
491 var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
492 var nestedMessage = Any.Pack(doubleNestedMessage);
493 var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
494 AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }",
495 formatter.Format(message));
496 }
497
498 [Test]
499 public void AnyUnknownType()
500 {
501 // The default type registry doesn't have any types in it.
502 var message = new TestAllTypes();
503 var any = Any.Pack(message);
504 Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(any));
505 }
506
507 /// <summary>
508 /// Checks that the actual JSON is the same as the expected JSON - but after replacing
509 /// all apostrophes in the expected JSON with double quotes. This basically makes the tests easier
510 /// to read.
511 /// </summary>
512 private static void AssertJson(string expectedJsonWithApostrophes, string actualJson)
513 {
514 var expectedJson = expectedJsonWithApostrophes.Replace("'", "\"");
515 Assert.AreEqual(expectedJson, actualJson);
516 }
517 }
518}