JsonFormatterTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #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. using System;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using System.Text;
  36. using System.Threading.Tasks;
  37. using Google.Protobuf.TestProtos;
  38. using NUnit.Framework;
  39. namespace Google.Protobuf
  40. {
  41. public class JsonFormatterTest
  42. {
  43. [Test]
  44. public void DefaultValues_WhenOmitted()
  45. {
  46. var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false));
  47. Assert.AreEqual("{ }", formatter.Format(new ForeignMessage()));
  48. Assert.AreEqual("{ }", formatter.Format(new TestAllTypes()));
  49. Assert.AreEqual("{ }", formatter.Format(new TestMap()));
  50. }
  51. [Test]
  52. public void DefaultValues_WhenIncluded()
  53. {
  54. var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true));
  55. Assert.AreEqual("{ \"c\": 0 }", formatter.Format(new ForeignMessage()));
  56. }
  57. [Test]
  58. public void AllSingleFields()
  59. {
  60. var message = new TestAllTypes
  61. {
  62. SingleBool = true,
  63. SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
  64. SingleDouble = 23.5,
  65. SingleFixed32 = 23,
  66. SingleFixed64 = 1234567890123,
  67. SingleFloat = 12.25f,
  68. SingleForeignEnum = ForeignEnum.FOREIGN_BAR,
  69. SingleForeignMessage = new ForeignMessage { C = 10 },
  70. SingleImportEnum = ImportEnum.IMPORT_BAZ,
  71. SingleImportMessage = new ImportMessage { D = 20 },
  72. SingleInt32 = 100,
  73. SingleInt64 = 3210987654321,
  74. SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO,
  75. SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
  76. SinglePublicImportMessage = new PublicImportMessage { E = 54 },
  77. SingleSfixed32 = -123,
  78. SingleSfixed64 = -12345678901234,
  79. SingleSint32 = -456,
  80. SingleSint64 = -12345678901235,
  81. SingleString = "test\twith\ttabs",
  82. SingleUint32 = uint.MaxValue,
  83. SingleUint64 = ulong.MaxValue,
  84. };
  85. var actualText = JsonFormatter.Default.Format(message);
  86. // Fields in declaration order, which matches numeric order.
  87. var expectedText = "{ " +
  88. "\"singleInt32\": 100, " +
  89. "\"singleInt64\": \"3210987654321\", " +
  90. "\"singleUint32\": 4294967295, " +
  91. "\"singleUint64\": \"18446744073709551615\", " +
  92. "\"singleSint32\": -456, " +
  93. "\"singleSint64\": \"-12345678901235\", " +
  94. "\"singleFixed32\": 23, " +
  95. "\"singleFixed64\": \"1234567890123\", " +
  96. "\"singleSfixed32\": -123, " +
  97. "\"singleSfixed64\": \"-12345678901234\", " +
  98. "\"singleFloat\": 12.25, " +
  99. "\"singleDouble\": 23.5, " +
  100. "\"singleBool\": true, " +
  101. "\"singleString\": \"test\\twith\\ttabs\", " +
  102. "\"singleBytes\": \"AQIDBA==\", " +
  103. "\"singleNestedMessage\": { \"bb\": 35 }, " +
  104. "\"singleForeignMessage\": { \"c\": 10 }, " +
  105. "\"singleImportMessage\": { \"d\": 20 }, " +
  106. "\"singleNestedEnum\": \"FOO\", " +
  107. "\"singleForeignEnum\": \"FOREIGN_BAR\", " +
  108. "\"singleImportEnum\": \"IMPORT_BAZ\", " +
  109. "\"singlePublicImportMessage\": { \"e\": 54 }" +
  110. " }";
  111. Assert.AreEqual(expectedText, actualText);
  112. }
  113. [Test]
  114. public void RepeatedField()
  115. {
  116. Assert.AreEqual("{ \"repeatedInt32\": [ 1, 2, 3, 4, 5 ] }",
  117. JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } }));
  118. }
  119. [Test]
  120. public void MapField_StringString()
  121. {
  122. Assert.AreEqual("{ \"mapStringString\": { \"with spaces\": \"bar\", \"a\": \"b\" } }",
  123. JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } }));
  124. }
  125. [Test]
  126. public void MapField_Int32Int32()
  127. {
  128. // The keys are quoted, but the values aren't.
  129. Assert.AreEqual("{ \"mapInt32Int32\": { \"0\": 1, \"2\": 3 } }",
  130. JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } }));
  131. }
  132. [Test]
  133. public void MapField_BoolBool()
  134. {
  135. // The keys are quoted, but the values aren't.
  136. Assert.AreEqual("{ \"mapBoolBool\": { \"false\": true, \"true\": false } }",
  137. JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } }));
  138. }
  139. [TestCase(1.0, "1")]
  140. [TestCase(double.NaN, "\"NaN\"")]
  141. [TestCase(double.PositiveInfinity, "\"Infinity\"")]
  142. [TestCase(double.NegativeInfinity, "\"-Infinity\"")]
  143. public void DoubleRepresentations(double value, string expectedValueText)
  144. {
  145. var message = new TestAllTypes { SingleDouble = value };
  146. string actualText = JsonFormatter.Default.Format(message);
  147. string expectedText = "{ \"singleDouble\": " + expectedValueText + " }";
  148. Assert.AreEqual(expectedText, actualText);
  149. }
  150. [Test]
  151. public void UnknownEnumValueOmitted_SingleField()
  152. {
  153. var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 };
  154. Assert.AreEqual("{ }", JsonFormatter.Default.Format(message));
  155. }
  156. [Test]
  157. public void UnknownEnumValueOmitted_RepeatedField()
  158. {
  159. var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } };
  160. Assert.AreEqual("{ \"repeatedForeignEnum\": [ \"FOREIGN_BAZ\", \"FOREIGN_FOO\" ] }", JsonFormatter.Default.Format(message));
  161. }
  162. [Test]
  163. public void UnknownEnumValueOmitted_MapField()
  164. {
  165. // This matches the C++ behaviour.
  166. var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } };
  167. Assert.AreEqual("{ \"mapInt32Enum\": { \"1\": \"MAP_ENUM_FOO\", \"3\": \"MAP_ENUM_BAR\" } }", JsonFormatter.Default.Format(message));
  168. }
  169. [Test]
  170. public void UnknownEnumValueOmitted_RepeatedField_AllEntriesUnknown()
  171. {
  172. // *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write...
  173. // but this is what happens at the moment, and it doesn't seem too awful.
  174. var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } };
  175. Assert.AreEqual("{ \"repeatedForeignEnum\": [ ] }", JsonFormatter.Default.Format(message));
  176. }
  177. [Test]
  178. public void NullValueForMessage()
  179. {
  180. var message = new TestMap { MapInt32ForeignMessage = { { 10, null } } };
  181. Assert.AreEqual("{ \"mapInt32ForeignMessage\": { \"10\": null } }", JsonFormatter.Default.Format(message));
  182. }
  183. [Test]
  184. [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit
  185. [TestCase("a\u0601b", "a\\u0601b")] // Ranged
  186. [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...)
  187. public void SimpleNonAscii(string text, string encoded)
  188. {
  189. var message = new TestAllTypes { SingleString = text };
  190. Assert.AreEqual("{ \"singleString\": \"" + encoded + "\" }", JsonFormatter.Default.Format(message));
  191. }
  192. [Test]
  193. public void SurrogatePairEscaping()
  194. {
  195. var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" };
  196. Assert.AreEqual("{ \"singleString\": \"a\\ud801\\udc01b\" }", JsonFormatter.Default.Format(message));
  197. }
  198. [Test]
  199. public void InvalidSurrogatePairsFail()
  200. {
  201. // Note: don't use TestCase for these, as the strings can't be reliably represented
  202. // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/
  203. // Lone low surrogate
  204. var message = new TestAllTypes { SingleString = "a\uDC01b" };
  205. Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
  206. // Lone high surrogate
  207. message = new TestAllTypes { SingleString = "a\uD801b" };
  208. Assert.Throws<ArgumentException>(() => JsonFormatter.Default.Format(message));
  209. }
  210. [Test]
  211. [TestCase("foo_bar", "fooBar")]
  212. [TestCase("bananaBanana", "bananaBanana")]
  213. [TestCase("BANANABanana", "bananaBanana")]
  214. public void ToCamelCase(string original, string expected)
  215. {
  216. Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original));
  217. }
  218. [Test]
  219. [TestCase(null, "{ }")]
  220. [TestCase("x", "{ \"fooString\": \"x\" }")]
  221. [TestCase("", "{ \"fooString\": \"\" }")]
  222. [TestCase(null, "{ }")]
  223. public void Oneof(string fooStringValue, string expectedJson)
  224. {
  225. var message = new TestOneof();
  226. if (fooStringValue != null)
  227. {
  228. message.FooString = fooStringValue;
  229. }
  230. // We should get the same result both with and without "format default values".
  231. var formatter = new JsonFormatter(new JsonFormatter.Settings(false));
  232. Assert.AreEqual(expectedJson, formatter.Format(message));
  233. formatter = new JsonFormatter(new JsonFormatter.Settings(true));
  234. Assert.AreEqual(expectedJson, formatter.Format(message));
  235. }
  236. }
  237. }