JsonParserTest.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 Google.Protobuf.Reflection;
  33. using Google.Protobuf.TestProtos;
  34. using Google.Protobuf.WellKnownTypes;
  35. using NUnit.Framework;
  36. using System;
  37. namespace Google.Protobuf
  38. {
  39. /// <summary>
  40. /// Unit tests for JSON parsing.
  41. /// </summary>
  42. public class JsonParserTest
  43. {
  44. // Sanity smoke test
  45. [Test]
  46. public void AllTypesRoundtrip()
  47. {
  48. AssertRoundtrip(SampleMessages.CreateFullTestAllTypes());
  49. }
  50. [Test]
  51. public void Maps()
  52. {
  53. AssertRoundtrip(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } });
  54. AssertRoundtrip(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } });
  55. AssertRoundtrip(new TestMap { MapBoolBool = { { false, true }, { true, false } } });
  56. }
  57. [Test]
  58. [TestCase(" 1 ")]
  59. [TestCase("+1")]
  60. [TestCase("1,000")]
  61. [TestCase("1.5")]
  62. public void IntegerMapKeysAreStrict(string keyText)
  63. {
  64. // Test that integer parsing is strict. We assume that if this is correct for int32,
  65. // it's correct for other numeric key types.
  66. var json = "{ \"mapInt32Int32\": { \"" + keyText + "\" : \"1\" } }";
  67. Assert.Throws<InvalidProtocolBufferException>(() => JsonParser.Default.Parse<TestMap>(json));
  68. }
  69. [Test]
  70. public void OriginalFieldNameAccepted()
  71. {
  72. var json = "{ \"single_int32\": 10 }";
  73. var expected = new TestAllTypes { SingleInt32 = 10 };
  74. Assert.AreEqual(expected, TestAllTypes.Parser.ParseJson(json));
  75. }
  76. [Test]
  77. public void SourceContextRoundtrip()
  78. {
  79. AssertRoundtrip(new SourceContext { FileName = "foo.proto" });
  80. }
  81. [Test]
  82. public void SingularWrappers_DefaultNonNullValues()
  83. {
  84. var message = new TestWellKnownTypes
  85. {
  86. StringField = "",
  87. BytesField = ByteString.Empty,
  88. BoolField = false,
  89. FloatField = 0f,
  90. DoubleField = 0d,
  91. Int32Field = 0,
  92. Int64Field = 0,
  93. Uint32Field = 0,
  94. Uint64Field = 0
  95. };
  96. AssertRoundtrip(message);
  97. }
  98. [Test]
  99. public void SingularWrappers_NonDefaultValues()
  100. {
  101. var message = new TestWellKnownTypes
  102. {
  103. StringField = "x",
  104. BytesField = ByteString.CopyFrom(1, 2, 3),
  105. BoolField = true,
  106. FloatField = 12.5f,
  107. DoubleField = 12.25d,
  108. Int32Field = 1,
  109. Int64Field = 2,
  110. Uint32Field = 3,
  111. Uint64Field = 4
  112. };
  113. AssertRoundtrip(message);
  114. }
  115. [Test]
  116. public void SingularWrappers_ExplicitNulls()
  117. {
  118. // When we parse the "valueField": null part, we remember it... basically, it's one case
  119. // where explicit default values don't fully roundtrip.
  120. var message = new TestWellKnownTypes { ValueField = Value.ForNull() };
  121. var json = new JsonFormatter(new JsonFormatter.Settings(true)).Format(message);
  122. var parsed = JsonParser.Default.Parse<TestWellKnownTypes>(json);
  123. Assert.AreEqual(message, parsed);
  124. }
  125. [Test]
  126. [TestCase(typeof(BoolValue), "true", true)]
  127. [TestCase(typeof(Int32Value), "32", 32)]
  128. [TestCase(typeof(Int64Value), "32", 32L)]
  129. [TestCase(typeof(Int64Value), "\"32\"", 32L)]
  130. [TestCase(typeof(UInt32Value), "32", 32U)]
  131. [TestCase(typeof(UInt64Value), "\"32\"", 32UL)]
  132. [TestCase(typeof(UInt64Value), "32", 32UL)]
  133. [TestCase(typeof(StringValue), "\"foo\"", "foo")]
  134. [TestCase(typeof(FloatValue), "1.5", 1.5f)]
  135. [TestCase(typeof(DoubleValue), "1.5", 1.5d)]
  136. public void Wrappers_Standalone(System.Type wrapperType, string json, object expectedValue)
  137. {
  138. IMessage parsed = (IMessage)Activator.CreateInstance(wrapperType);
  139. IMessage expected = (IMessage)Activator.CreateInstance(wrapperType);
  140. JsonParser.Default.Merge(parsed, "null");
  141. Assert.AreEqual(expected, parsed);
  142. JsonParser.Default.Merge(parsed, json);
  143. expected.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumber].Accessor.SetValue(expected, expectedValue);
  144. Assert.AreEqual(expected, parsed);
  145. }
  146. [Test]
  147. public void ExplicitNullValue()
  148. {
  149. string json = "{\"valueField\": null}";
  150. var message = JsonParser.Default.Parse<TestWellKnownTypes>(json);
  151. Assert.AreEqual(new TestWellKnownTypes { ValueField = Value.ForNull() }, message);
  152. }
  153. [Test]
  154. public void BytesWrapper_Standalone()
  155. {
  156. ByteString data = ByteString.CopyFrom(1, 2, 3);
  157. // Can't do this with attributes...
  158. var parsed = JsonParser.Default.Parse<BytesValue>(WrapInQuotes(data.ToBase64()));
  159. var expected = new BytesValue { Value = data };
  160. Assert.AreEqual(expected, parsed);
  161. }
  162. [Test]
  163. public void RepeatedWrappers()
  164. {
  165. var message = new RepeatedWellKnownTypes
  166. {
  167. BoolField = { true, false },
  168. BytesField = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom(4, 5, 6), ByteString.Empty },
  169. DoubleField = { 12.5, -1.5, 0d },
  170. FloatField = { 123.25f, -20f, 0f },
  171. Int32Field = { int.MaxValue, int.MinValue, 0 },
  172. Int64Field = { long.MaxValue, long.MinValue, 0L },
  173. StringField = { "First", "Second", "" },
  174. Uint32Field = { uint.MaxValue, uint.MinValue, 0U },
  175. Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL },
  176. };
  177. AssertRoundtrip(message);
  178. }
  179. [Test]
  180. public void RepeatedField_NullElementProhibited()
  181. {
  182. string json = "{ \"repeated_foreign_message\": [null] }";
  183. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  184. }
  185. [Test]
  186. public void RepeatedField_NullOverallValueAllowed()
  187. {
  188. string json = "{ \"repeated_foreign_message\": null }";
  189. Assert.AreEqual(new TestAllTypes(), TestAllTypes.Parser.ParseJson(json));
  190. }
  191. [Test]
  192. [TestCase("{ \"mapInt32Int32\": { \"10\": null }")]
  193. [TestCase("{ \"mapStringString\": { \"abc\": null }")]
  194. [TestCase("{ \"mapInt32ForeignMessage\": { \"10\": null }")]
  195. public void MapField_NullValueProhibited(string json)
  196. {
  197. Assert.Throws<InvalidProtocolBufferException>(() => TestMap.Parser.ParseJson(json));
  198. }
  199. [Test]
  200. public void MapField_NullOverallValueAllowed()
  201. {
  202. string json = "{ \"mapInt32Int32\": null }";
  203. Assert.AreEqual(new TestMap(), TestMap.Parser.ParseJson(json));
  204. }
  205. [Test]
  206. public void IndividualWrapperTypes()
  207. {
  208. Assert.AreEqual(new StringValue { Value = "foo" }, StringValue.Parser.ParseJson("\"foo\""));
  209. Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.ParseJson("1"));
  210. // Can parse strings directly too
  211. Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.ParseJson("\"1\""));
  212. }
  213. private static void AssertRoundtrip<T>(T message) where T : IMessage<T>, new()
  214. {
  215. var clone = message.Clone();
  216. var json = JsonFormatter.Default.Format(message);
  217. var parsed = JsonParser.Default.Parse<T>(json);
  218. Assert.AreEqual(clone, parsed);
  219. }
  220. [Test]
  221. [TestCase("0", 0)]
  222. [TestCase("-0", 0)] // Not entirely clear whether we intend to allow this...
  223. [TestCase("1", 1)]
  224. [TestCase("-1", -1)]
  225. [TestCase("2147483647", 2147483647)]
  226. [TestCase("-2147483648", -2147483648)]
  227. public void StringToInt32_Valid(string jsonValue, int expectedParsedValue)
  228. {
  229. string json = "{ \"singleInt32\": \"" + jsonValue + "\"}";
  230. var parsed = TestAllTypes.Parser.ParseJson(json);
  231. Assert.AreEqual(expectedParsedValue, parsed.SingleInt32);
  232. }
  233. [Test]
  234. [TestCase("+0")]
  235. [TestCase(" 1")]
  236. [TestCase("1 ")]
  237. [TestCase("00")]
  238. [TestCase("-00")]
  239. [TestCase("--1")]
  240. [TestCase("+1")]
  241. [TestCase("1.5")]
  242. [TestCase("1e10")]
  243. [TestCase("2147483648")]
  244. [TestCase("-2147483649")]
  245. public void StringToInt32_Invalid(string jsonValue)
  246. {
  247. string json = "{ \"singleInt32\": \"" + jsonValue + "\"}";
  248. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  249. }
  250. [Test]
  251. [TestCase("0", 0U)]
  252. [TestCase("1", 1U)]
  253. [TestCase("4294967295", 4294967295U)]
  254. public void StringToUInt32_Valid(string jsonValue, uint expectedParsedValue)
  255. {
  256. string json = "{ \"singleUint32\": \"" + jsonValue + "\"}";
  257. var parsed = TestAllTypes.Parser.ParseJson(json);
  258. Assert.AreEqual(expectedParsedValue, parsed.SingleUint32);
  259. }
  260. // Assume that anything non-bounds-related is covered in the Int32 case
  261. [Test]
  262. [TestCase("-1")]
  263. [TestCase("4294967296")]
  264. public void StringToUInt32_Invalid(string jsonValue)
  265. {
  266. string json = "{ \"singleUint32\": \"" + jsonValue + "\"}";
  267. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  268. }
  269. [Test]
  270. [TestCase("0", 0L)]
  271. [TestCase("1", 1L)]
  272. [TestCase("-1", -1L)]
  273. [TestCase("9223372036854775807", 9223372036854775807)]
  274. [TestCase("-9223372036854775808", -9223372036854775808)]
  275. public void StringToInt64_Valid(string jsonValue, long expectedParsedValue)
  276. {
  277. string json = "{ \"singleInt64\": \"" + jsonValue + "\"}";
  278. var parsed = TestAllTypes.Parser.ParseJson(json);
  279. Assert.AreEqual(expectedParsedValue, parsed.SingleInt64);
  280. }
  281. // Assume that anything non-bounds-related is covered in the Int32 case
  282. [Test]
  283. [TestCase("-9223372036854775809")]
  284. [TestCase("9223372036854775808")]
  285. public void StringToInt64_Invalid(string jsonValue)
  286. {
  287. string json = "{ \"singleInt64\": \"" + jsonValue + "\"}";
  288. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  289. }
  290. [Test]
  291. [TestCase("0", 0UL)]
  292. [TestCase("1", 1UL)]
  293. [TestCase("18446744073709551615", 18446744073709551615)]
  294. public void StringToUInt64_Valid(string jsonValue, ulong expectedParsedValue)
  295. {
  296. string json = "{ \"singleUint64\": \"" + jsonValue + "\"}";
  297. var parsed = TestAllTypes.Parser.ParseJson(json);
  298. Assert.AreEqual(expectedParsedValue, parsed.SingleUint64);
  299. }
  300. // Assume that anything non-bounds-related is covered in the Int32 case
  301. [Test]
  302. [TestCase("-1")]
  303. [TestCase("18446744073709551616")]
  304. public void StringToUInt64_Invalid(string jsonValue)
  305. {
  306. string json = "{ \"singleUint64\": \"" + jsonValue + "\"}";
  307. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  308. }
  309. [Test]
  310. [TestCase("0", 0d)]
  311. [TestCase("1", 1d)]
  312. [TestCase("1.000000", 1d)]
  313. [TestCase("1.0000000000000000000000001", 1d)] // We don't notice that we haven't preserved the exact value
  314. [TestCase("-1", -1d)]
  315. [TestCase("1e1", 10d)]
  316. [TestCase("1e01", 10d)] // Leading decimals are allowed in exponents
  317. [TestCase("1E1", 10d)] // Either case is fine
  318. [TestCase("-1e1", -10d)]
  319. [TestCase("1.5e1", 15d)]
  320. [TestCase("-1.5e1", -15d)]
  321. [TestCase("15e-1", 1.5d)]
  322. [TestCase("-15e-1", -1.5d)]
  323. [TestCase("1.79769e308", 1.79769e308)]
  324. [TestCase("-1.79769e308", -1.79769e308)]
  325. [TestCase("Infinity", double.PositiveInfinity)]
  326. [TestCase("-Infinity", double.NegativeInfinity)]
  327. [TestCase("NaN", double.NaN)]
  328. public void StringToDouble_Valid(string jsonValue, double expectedParsedValue)
  329. {
  330. string json = "{ \"singleDouble\": \"" + jsonValue + "\"}";
  331. var parsed = TestAllTypes.Parser.ParseJson(json);
  332. Assert.AreEqual(expectedParsedValue, parsed.SingleDouble);
  333. }
  334. [Test]
  335. [TestCase("1.7977e308")]
  336. [TestCase("-1.7977e308")]
  337. [TestCase("1e309")]
  338. [TestCase("1,0")]
  339. [TestCase("1.0.0")]
  340. [TestCase("+1")]
  341. [TestCase("00")]
  342. [TestCase("01")]
  343. [TestCase("-00")]
  344. [TestCase("-01")]
  345. [TestCase("--1")]
  346. [TestCase(" Infinity")]
  347. [TestCase(" -Infinity")]
  348. [TestCase("NaN ")]
  349. [TestCase("Infinity ")]
  350. [TestCase("-Infinity ")]
  351. [TestCase(" NaN")]
  352. [TestCase("INFINITY")]
  353. [TestCase("nan")]
  354. [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity checking...
  355. public void StringToDouble_Invalid(string jsonValue)
  356. {
  357. string json = "{ \"singleDouble\": \"" + jsonValue + "\"}";
  358. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  359. }
  360. [Test]
  361. [TestCase("0", 0f)]
  362. [TestCase("1", 1f)]
  363. [TestCase("1.000000", 1f)]
  364. [TestCase("-1", -1f)]
  365. [TestCase("3.402823e38", 3.402823e38f)]
  366. [TestCase("-3.402823e38", -3.402823e38f)]
  367. [TestCase("1.5e1", 15f)]
  368. [TestCase("15e-1", 1.5f)]
  369. public void StringToFloat_Valid(string jsonValue, float expectedParsedValue)
  370. {
  371. string json = "{ \"singleFloat\": \"" + jsonValue + "\"}";
  372. var parsed = TestAllTypes.Parser.ParseJson(json);
  373. Assert.AreEqual(expectedParsedValue, parsed.SingleFloat);
  374. }
  375. [Test]
  376. [TestCase("3.402824e38")]
  377. [TestCase("-3.402824e38")]
  378. [TestCase("1,0")]
  379. [TestCase("1.0.0")]
  380. [TestCase("+1")]
  381. [TestCase("00")]
  382. [TestCase("--1")]
  383. public void StringToFloat_Invalid(string jsonValue)
  384. {
  385. string json = "{ \"singleFloat\": \"" + jsonValue + "\"}";
  386. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  387. }
  388. [Test]
  389. [TestCase("0", 0)]
  390. [TestCase("-0", 0)] // Not entirely clear whether we intend to allow this...
  391. [TestCase("1", 1)]
  392. [TestCase("-1", -1)]
  393. [TestCase("2147483647", 2147483647)]
  394. [TestCase("-2147483648", -2147483648)]
  395. [TestCase("1e1", 10)]
  396. [TestCase("-1e1", -10)]
  397. [TestCase("10.00", 10)]
  398. [TestCase("-10.00", -10)]
  399. public void NumberToInt32_Valid(string jsonValue, int expectedParsedValue)
  400. {
  401. string json = "{ \"singleInt32\": " + jsonValue + "}";
  402. var parsed = TestAllTypes.Parser.ParseJson(json);
  403. Assert.AreEqual(expectedParsedValue, parsed.SingleInt32);
  404. }
  405. [Test]
  406. [TestCase("+0", typeof(InvalidJsonException))]
  407. [TestCase("00", typeof(InvalidJsonException))]
  408. [TestCase("-00", typeof(InvalidJsonException))]
  409. [TestCase("--1", typeof(InvalidJsonException))]
  410. [TestCase("+1", typeof(InvalidJsonException))]
  411. [TestCase("1.5", typeof(InvalidProtocolBufferException))]
  412. // Value is out of range
  413. [TestCase("1e10", typeof(InvalidProtocolBufferException))]
  414. [TestCase("2147483648", typeof(InvalidProtocolBufferException))]
  415. [TestCase("-2147483649", typeof(InvalidProtocolBufferException))]
  416. public void NumberToInt32_Invalid(string jsonValue, System.Type expectedExceptionType)
  417. {
  418. string json = "{ \"singleInt32\": " + jsonValue + "}";
  419. Assert.Throws(expectedExceptionType, () => TestAllTypes.Parser.ParseJson(json));
  420. }
  421. [Test]
  422. [TestCase("0", 0U)]
  423. [TestCase("1", 1U)]
  424. [TestCase("4294967295", 4294967295U)]
  425. public void NumberToUInt32_Valid(string jsonValue, uint expectedParsedValue)
  426. {
  427. string json = "{ \"singleUint32\": " + jsonValue + "}";
  428. var parsed = TestAllTypes.Parser.ParseJson(json);
  429. Assert.AreEqual(expectedParsedValue, parsed.SingleUint32);
  430. }
  431. // Assume that anything non-bounds-related is covered in the Int32 case
  432. [Test]
  433. [TestCase("-1")]
  434. [TestCase("4294967296")]
  435. public void NumberToUInt32_Invalid(string jsonValue)
  436. {
  437. string json = "{ \"singleUint32\": " + jsonValue + "}";
  438. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  439. }
  440. [Test]
  441. [TestCase("0", 0L)]
  442. [TestCase("1", 1L)]
  443. [TestCase("-1", -1L)]
  444. // long.MaxValue isn't actually representable as a double. This string value is the highest
  445. // representable value which isn't greater than long.MaxValue.
  446. [TestCase("9223372036854774784", 9223372036854774784)]
  447. [TestCase("-9223372036854775808", -9223372036854775808)]
  448. public void NumberToInt64_Valid(string jsonValue, long expectedParsedValue)
  449. {
  450. string json = "{ \"singleInt64\": " + jsonValue + "}";
  451. var parsed = TestAllTypes.Parser.ParseJson(json);
  452. Assert.AreEqual(expectedParsedValue, parsed.SingleInt64);
  453. }
  454. // Assume that anything non-bounds-related is covered in the Int32 case
  455. [Test]
  456. [TestCase("9223372036854775808")]
  457. // Theoretical bound would be -9223372036854775809, but when that is parsed to a double
  458. // we end up with the exact value of long.MinValue due to lack of precision. The value here
  459. // is the "next double down".
  460. [TestCase("-9223372036854780000")]
  461. public void NumberToInt64_Invalid(string jsonValue)
  462. {
  463. string json = "{ \"singleInt64\": " + jsonValue + "}";
  464. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  465. }
  466. [Test]
  467. [TestCase("0", 0UL)]
  468. [TestCase("1", 1UL)]
  469. // ulong.MaxValue isn't representable as a double. This value is the largest double within
  470. // the range of ulong.
  471. [TestCase("18446744073709549568", 18446744073709549568UL)]
  472. public void NumberToUInt64_Valid(string jsonValue, ulong expectedParsedValue)
  473. {
  474. string json = "{ \"singleUint64\": " + jsonValue + "}";
  475. var parsed = TestAllTypes.Parser.ParseJson(json);
  476. Assert.AreEqual(expectedParsedValue, parsed.SingleUint64);
  477. }
  478. // Assume that anything non-bounds-related is covered in the Int32 case
  479. [Test]
  480. [TestCase("-1")]
  481. [TestCase("18446744073709551616")]
  482. public void NumberToUInt64_Invalid(string jsonValue)
  483. {
  484. string json = "{ \"singleUint64\": " + jsonValue + "}";
  485. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  486. }
  487. [Test]
  488. [TestCase("0", 0d)]
  489. [TestCase("1", 1d)]
  490. [TestCase("1.000000", 1d)]
  491. [TestCase("1.0000000000000000000000001", 1d)] // We don't notice that we haven't preserved the exact value
  492. [TestCase("-1", -1d)]
  493. [TestCase("1e1", 10d)]
  494. [TestCase("1e01", 10d)] // Leading decimals are allowed in exponents
  495. [TestCase("1E1", 10d)] // Either case is fine
  496. [TestCase("-1e1", -10d)]
  497. [TestCase("1.5e1", 15d)]
  498. [TestCase("-1.5e1", -15d)]
  499. [TestCase("15e-1", 1.5d)]
  500. [TestCase("-15e-1", -1.5d)]
  501. [TestCase("1.79769e308", 1.79769e308)]
  502. [TestCase("-1.79769e308", -1.79769e308)]
  503. public void NumberToDouble_Valid(string jsonValue, double expectedParsedValue)
  504. {
  505. string json = "{ \"singleDouble\": " + jsonValue + "}";
  506. var parsed = TestAllTypes.Parser.ParseJson(json);
  507. Assert.AreEqual(expectedParsedValue, parsed.SingleDouble);
  508. }
  509. [Test]
  510. [TestCase("1.7977e308")]
  511. [TestCase("-1.7977e308")]
  512. [TestCase("1e309")]
  513. [TestCase("1,0")]
  514. [TestCase("1.0.0")]
  515. [TestCase("+1")]
  516. [TestCase("00")]
  517. [TestCase("--1")]
  518. [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity checking...
  519. public void NumberToDouble_Invalid(string jsonValue)
  520. {
  521. string json = "{ \"singleDouble\": " + jsonValue + "}";
  522. Assert.Throws<InvalidJsonException>(() => TestAllTypes.Parser.ParseJson(json));
  523. }
  524. [Test]
  525. [TestCase("0", 0f)]
  526. [TestCase("1", 1f)]
  527. [TestCase("1.000000", 1f)]
  528. [TestCase("-1", -1f)]
  529. [TestCase("3.402823e38", 3.402823e38f)]
  530. [TestCase("-3.402823e38", -3.402823e38f)]
  531. [TestCase("1.5e1", 15f)]
  532. [TestCase("15e-1", 1.5f)]
  533. public void NumberToFloat_Valid(string jsonValue, float expectedParsedValue)
  534. {
  535. string json = "{ \"singleFloat\": " + jsonValue + "}";
  536. var parsed = TestAllTypes.Parser.ParseJson(json);
  537. Assert.AreEqual(expectedParsedValue, parsed.SingleFloat);
  538. }
  539. [Test]
  540. [TestCase("3.402824e38", typeof(InvalidProtocolBufferException))]
  541. [TestCase("-3.402824e38", typeof(InvalidProtocolBufferException))]
  542. [TestCase("1,0", typeof(InvalidJsonException))]
  543. [TestCase("1.0.0", typeof(InvalidJsonException))]
  544. [TestCase("+1", typeof(InvalidJsonException))]
  545. [TestCase("00", typeof(InvalidJsonException))]
  546. [TestCase("--1", typeof(InvalidJsonException))]
  547. public void NumberToFloat_Invalid(string jsonValue, System.Type expectedExceptionType)
  548. {
  549. string json = "{ \"singleFloat\": " + jsonValue + "}";
  550. Assert.Throws(expectedExceptionType, () => TestAllTypes.Parser.ParseJson(json));
  551. }
  552. // The simplest way of testing that the value has parsed correctly is to reformat it,
  553. // as we trust the formatting. In many cases that will give the same result as the input,
  554. // so in those cases we accept an expectedFormatted value of null. Sometimes the results
  555. // will be different though, due to a different number of digits being provided.
  556. [Test]
  557. // Z offset
  558. [TestCase("2015-10-09T14:46:23.123456789Z", null)]
  559. [TestCase("2015-10-09T14:46:23.123456Z", null)]
  560. [TestCase("2015-10-09T14:46:23.123Z", null)]
  561. [TestCase("2015-10-09T14:46:23Z", null)]
  562. [TestCase("2015-10-09T14:46:23.123456000Z", "2015-10-09T14:46:23.123456Z")]
  563. [TestCase("2015-10-09T14:46:23.1234560Z", "2015-10-09T14:46:23.123456Z")]
  564. [TestCase("2015-10-09T14:46:23.123000000Z", "2015-10-09T14:46:23.123Z")]
  565. [TestCase("2015-10-09T14:46:23.1230Z", "2015-10-09T14:46:23.123Z")]
  566. [TestCase("2015-10-09T14:46:23.00Z", "2015-10-09T14:46:23Z")]
  567. // +00:00 offset
  568. [TestCase("2015-10-09T14:46:23.123456789+00:00", "2015-10-09T14:46:23.123456789Z")]
  569. [TestCase("2015-10-09T14:46:23.123456+00:00", "2015-10-09T14:46:23.123456Z")]
  570. [TestCase("2015-10-09T14:46:23.123+00:00", "2015-10-09T14:46:23.123Z")]
  571. [TestCase("2015-10-09T14:46:23+00:00", "2015-10-09T14:46:23Z")]
  572. [TestCase("2015-10-09T14:46:23.123456000+00:00", "2015-10-09T14:46:23.123456Z")]
  573. [TestCase("2015-10-09T14:46:23.1234560+00:00", "2015-10-09T14:46:23.123456Z")]
  574. [TestCase("2015-10-09T14:46:23.123000000+00:00", "2015-10-09T14:46:23.123Z")]
  575. [TestCase("2015-10-09T14:46:23.1230+00:00", "2015-10-09T14:46:23.123Z")]
  576. [TestCase("2015-10-09T14:46:23.00+00:00", "2015-10-09T14:46:23Z")]
  577. // Other offsets (assume by now that the subsecond handling is okay)
  578. [TestCase("2015-10-09T15:46:23.123456789+01:00", "2015-10-09T14:46:23.123456789Z")]
  579. [TestCase("2015-10-09T13:46:23.123456789-01:00", "2015-10-09T14:46:23.123456789Z")]
  580. [TestCase("2015-10-09T15:16:23.123456789+00:30", "2015-10-09T14:46:23.123456789Z")]
  581. [TestCase("2015-10-09T14:16:23.123456789-00:30", "2015-10-09T14:46:23.123456789Z")]
  582. [TestCase("2015-10-09T16:31:23.123456789+01:45", "2015-10-09T14:46:23.123456789Z")]
  583. [TestCase("2015-10-09T13:01:23.123456789-01:45", "2015-10-09T14:46:23.123456789Z")]
  584. [TestCase("2015-10-10T08:46:23.123456789+18:00", "2015-10-09T14:46:23.123456789Z")]
  585. [TestCase("2015-10-08T20:46:23.123456789-18:00", "2015-10-09T14:46:23.123456789Z")]
  586. // Leap years and min/max
  587. [TestCase("2016-02-29T14:46:23.123456789Z", null)]
  588. [TestCase("2000-02-29T14:46:23.123456789Z", null)]
  589. [TestCase("0001-01-01T00:00:00Z", null)]
  590. [TestCase("9999-12-31T23:59:59.999999999Z", null)]
  591. public void Timestamp_Valid(string jsonValue, string expectedFormatted)
  592. {
  593. expectedFormatted = expectedFormatted ?? jsonValue;
  594. string json = WrapInQuotes(jsonValue);
  595. var parsed = Timestamp.Parser.ParseJson(json);
  596. Assert.AreEqual(WrapInQuotes(expectedFormatted), parsed.ToString());
  597. }
  598. [Test]
  599. [TestCase("2015-10-09 14:46:23.123456789Z", Description = "No T between date and time")]
  600. [TestCase("2015/10/09T14:46:23.123456789Z", Description = "Wrong date separators")]
  601. [TestCase("2015-10-09T14.46.23.123456789Z", Description = "Wrong time separators")]
  602. [TestCase("2015-10-09T14:46:23,123456789Z", Description = "Wrong fractional second separators (valid ISO-8601 though)")]
  603. [TestCase(" 2015-10-09T14:46:23.123456789Z", Description = "Whitespace at start")]
  604. [TestCase("2015-10-09T14:46:23.123456789Z ", Description = "Whitespace at end")]
  605. [TestCase("2015-10-09T14:46:23.1234567890", Description = "Too many digits")]
  606. [TestCase("2015-10-09T14:46:23.123456789", Description = "No offset")]
  607. [TestCase("2015-13-09T14:46:23.123456789Z", Description = "Invalid month")]
  608. [TestCase("2015-10-32T14:46:23.123456789Z", Description = "Invalid day")]
  609. [TestCase("2015-10-09T24:00:00.000000000Z", Description = "Invalid hour (valid ISO-8601 though)")]
  610. [TestCase("2015-10-09T14:60:23.123456789Z", Description = "Invalid minutes")]
  611. [TestCase("2015-10-09T14:46:60.123456789Z", Description = "Invalid seconds")]
  612. [TestCase("2015-10-09T14:46:23.123456789+18:01", Description = "Offset too large (positive)")]
  613. [TestCase("2015-10-09T14:46:23.123456789-18:01", Description = "Offset too large (negative)")]
  614. [TestCase("2015-10-09T14:46:23.123456789-00:00", Description = "Local offset (-00:00) makes no sense here")]
  615. [TestCase("0001-01-01T00:00:00+00:01", Description = "Value before earliest when offset applied")]
  616. [TestCase("9999-12-31T23:59:59.999999999-00:01", Description = "Value after latest when offset applied")]
  617. [TestCase("2100-02-29T14:46:23.123456789Z", Description = "Feb 29th on a non-leap-year")]
  618. public void Timestamp_Invalid(string jsonValue)
  619. {
  620. string json = WrapInQuotes(jsonValue);
  621. Assert.Throws<InvalidProtocolBufferException>(() => Timestamp.Parser.ParseJson(json));
  622. }
  623. [Test]
  624. public void StructValue_Null()
  625. {
  626. Assert.AreEqual(new Value { NullValue = 0 }, Value.Parser.ParseJson("null"));
  627. }
  628. [Test]
  629. public void StructValue_String()
  630. {
  631. Assert.AreEqual(new Value { StringValue = "hi" }, Value.Parser.ParseJson("\"hi\""));
  632. }
  633. [Test]
  634. public void StructValue_Bool()
  635. {
  636. Assert.AreEqual(new Value { BoolValue = true }, Value.Parser.ParseJson("true"));
  637. Assert.AreEqual(new Value { BoolValue = false }, Value.Parser.ParseJson("false"));
  638. }
  639. [Test]
  640. public void StructValue_List()
  641. {
  642. Assert.AreEqual(Value.ForList(Value.ForNumber(1), Value.ForString("x")), Value.Parser.ParseJson("[1, \"x\"]"));
  643. }
  644. [Test]
  645. public void ParseListValue()
  646. {
  647. Assert.AreEqual(new ListValue { Values = { Value.ForNumber(1), Value.ForString("x") } }, ListValue.Parser.ParseJson("[1, \"x\"]"));
  648. }
  649. [Test]
  650. public void StructValue_Struct()
  651. {
  652. Assert.AreEqual(
  653. Value.ForStruct(new Struct { Fields = { { "x", Value.ForNumber(1) }, { "y", Value.ForString("z") } } }),
  654. Value.Parser.ParseJson("{ \"x\": 1, \"y\": \"z\" }"));
  655. }
  656. [Test]
  657. public void ParseStruct()
  658. {
  659. Assert.AreEqual(new Struct { Fields = { { "x", Value.ForNumber(1) }, { "y", Value.ForString("z") } } },
  660. Struct.Parser.ParseJson("{ \"x\": 1, \"y\": \"z\" }"));
  661. }
  662. // TODO for duration parsing: upper and lower bounds.
  663. // +/- 315576000000 seconds
  664. [Test]
  665. [TestCase("1.123456789s", null)]
  666. [TestCase("1.123456s", null)]
  667. [TestCase("1.123s", null)]
  668. [TestCase("1.12300s", "1.123s")]
  669. [TestCase("1.12345s", "1.123450s")]
  670. [TestCase("1s", null)]
  671. [TestCase("-1.123456789s", null)]
  672. [TestCase("-1.123456s", null)]
  673. [TestCase("-1.123s", null)]
  674. [TestCase("-1s", null)]
  675. [TestCase("0.123s", null)]
  676. [TestCase("-0.123s", null)]
  677. [TestCase("123456.123s", null)]
  678. [TestCase("-123456.123s", null)]
  679. // Upper and lower bounds
  680. [TestCase("315576000000s", null)]
  681. [TestCase("-315576000000s", null)]
  682. public void Duration_Valid(string jsonValue, string expectedFormatted)
  683. {
  684. expectedFormatted = expectedFormatted ?? jsonValue;
  685. string json = WrapInQuotes(jsonValue);
  686. var parsed = Duration.Parser.ParseJson(json);
  687. Assert.AreEqual(WrapInQuotes(expectedFormatted), parsed.ToString());
  688. }
  689. // The simplest way of testing that the value has parsed correctly is to reformat it,
  690. // as we trust the formatting. In many cases that will give the same result as the input,
  691. // so in those cases we accept an expectedFormatted value of null. Sometimes the results
  692. // will be different though, due to a different number of digits being provided.
  693. [Test]
  694. [TestCase("1.1234567890s", Description = "Too many digits")]
  695. [TestCase("1.123456789", Description = "No suffix")]
  696. [TestCase("1.123456789ss", Description = "Too much suffix")]
  697. [TestCase("1.123456789S", Description = "Upper case suffix")]
  698. [TestCase("+1.123456789s", Description = "Leading +")]
  699. [TestCase(".123456789s", Description = "No integer before the fraction")]
  700. [TestCase("1,123456789s", Description = "Comma as decimal separator")]
  701. [TestCase("1x1.123456789s", Description = "Non-digit in integer part")]
  702. [TestCase("1.1x3456789s", Description = "Non-digit in fractional part")]
  703. [TestCase(" 1.123456789s", Description = "Whitespace before fraction")]
  704. [TestCase("1.123456789s ", Description = "Whitespace after value")]
  705. [TestCase("01.123456789s", Description = "Leading zero (positive)")]
  706. [TestCase("-01.123456789s", Description = "Leading zero (negative)")]
  707. [TestCase("--0.123456789s", Description = "Double minus sign")]
  708. // Violate upper/lower bounds in various ways
  709. [TestCase("315576000001s", Description = "Integer part too large")]
  710. [TestCase("3155760000000s", Description = "Integer part too long (positive)")]
  711. [TestCase("-3155760000000s", Description = "Integer part too long (negative)")]
  712. public void Duration_Invalid(string jsonValue)
  713. {
  714. string json = WrapInQuotes(jsonValue);
  715. Assert.Throws<InvalidProtocolBufferException>(() => Duration.Parser.ParseJson(json));
  716. }
  717. // Not as many tests for field masks as I'd like; more to be added when we have more
  718. // detailed specifications.
  719. [Test]
  720. [TestCase("")]
  721. [TestCase("foo", "foo")]
  722. [TestCase("foo,bar", "foo", "bar")]
  723. [TestCase("foo.bar", "foo.bar")]
  724. [TestCase("fooBar", "foo_bar")]
  725. [TestCase("fooBar.bazQux", "foo_bar.baz_qux")]
  726. public void FieldMask_Valid(string jsonValue, params string[] expectedPaths)
  727. {
  728. string json = WrapInQuotes(jsonValue);
  729. var parsed = FieldMask.Parser.ParseJson(json);
  730. CollectionAssert.AreEqual(expectedPaths, parsed.Paths);
  731. }
  732. [Test]
  733. [TestCase("foo_bar")]
  734. public void FieldMask_Invalid(string jsonValue)
  735. {
  736. string json = WrapInQuotes(jsonValue);
  737. Assert.Throws<InvalidProtocolBufferException>(() => FieldMask.Parser.ParseJson(json));
  738. }
  739. [Test]
  740. public void Any_RegularMessage()
  741. {
  742. var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor);
  743. var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor)));
  744. var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } };
  745. var original = Any.Pack(message);
  746. var json = formatter.Format(original); // This is tested in JsonFormatterTest
  747. var parser = new JsonParser(new JsonParser.Settings(10, registry));
  748. Assert.AreEqual(original, parser.Parse<Any>(json));
  749. string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessage\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest3.TestAllTypes\" }";
  750. Assert.AreEqual(original, parser.Parse<Any>(valueFirstJson));
  751. }
  752. [Test]
  753. public void Any_CustomPrefix()
  754. {
  755. var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor);
  756. var message = new TestAllTypes { SingleInt32 = 10 };
  757. var original = Any.Pack(message, "custom.prefix/middle-part");
  758. var parser = new JsonParser(new JsonParser.Settings(10, registry));
  759. string json = "{ \"@type\": \"custom.prefix/middle-part/protobuf_unittest3.TestAllTypes\", \"singleInt32\": 10 }";
  760. Assert.AreEqual(original, parser.Parse<Any>(json));
  761. }
  762. [Test]
  763. public void Any_UnknownType()
  764. {
  765. string json = "{ \"@type\": \"type.googleapis.com/bogus\" }";
  766. Assert.Throws<InvalidOperationException>(() => Any.Parser.ParseJson(json));
  767. }
  768. [Test]
  769. public void Any_NoTypeUrl()
  770. {
  771. string json = "{ \"foo\": \"bar\" }";
  772. Assert.Throws<InvalidProtocolBufferException>(() => Any.Parser.ParseJson(json));
  773. }
  774. [Test]
  775. public void Any_WellKnownType()
  776. {
  777. var registry = TypeRegistry.FromMessages(Timestamp.Descriptor);
  778. var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));
  779. var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp();
  780. var original = Any.Pack(timestamp);
  781. var json = formatter.Format(original); // This is tested in JsonFormatterTest
  782. var parser = new JsonParser(new JsonParser.Settings(10, registry));
  783. Assert.AreEqual(original, parser.Parse<Any>(json));
  784. string valueFirstJson = "{ \"value\": \"1673-06-19T12:34:56Z\", \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\" }";
  785. Assert.AreEqual(original, parser.Parse<Any>(valueFirstJson));
  786. }
  787. [Test]
  788. public void Any_Nested()
  789. {
  790. var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor);
  791. var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry));
  792. var parser = new JsonParser(new JsonParser.Settings(10, registry));
  793. var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 };
  794. var nestedMessage = Any.Pack(doubleNestedMessage);
  795. var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) };
  796. var json = formatter.Format(message);
  797. // Use the descriptor-based parser just for a change.
  798. Assert.AreEqual(message, parser.Parse(json, TestWellKnownTypes.Descriptor));
  799. }
  800. [Test]
  801. public void DataAfterObject()
  802. {
  803. string json = "{} 10";
  804. Assert.Throws<InvalidJsonException>(() => TestAllTypes.Parser.ParseJson(json));
  805. }
  806. /// <summary>
  807. /// JSON equivalent to <see cref="CodedInputStreamTest.MaliciousRecursion"/>
  808. /// </summary>
  809. [Test]
  810. public void MaliciousRecursion()
  811. {
  812. string data64 = CodedInputStreamTest.MakeRecursiveMessage(64).ToString();
  813. string data65 = CodedInputStreamTest.MakeRecursiveMessage(65).ToString();
  814. var parser64 = new JsonParser(new JsonParser.Settings(64));
  815. CodedInputStreamTest.AssertMessageDepth(parser64.Parse<TestRecursiveMessage>(data64), 64);
  816. Assert.Throws<InvalidProtocolBufferException>(() => parser64.Parse<TestRecursiveMessage>(data65));
  817. var parser63 = new JsonParser(new JsonParser.Settings(63));
  818. Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<TestRecursiveMessage>(data64));
  819. }
  820. [Test]
  821. [TestCase("AQI")]
  822. [TestCase("_-==")]
  823. public void Bytes_InvalidBase64(string badBase64)
  824. {
  825. string json = "{ \"singleBytes\": \"" + badBase64 + "\" }";
  826. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  827. }
  828. [Test]
  829. [TestCase("\"FOREIGN_BAR\"", ForeignEnum.ForeignBar)]
  830. [TestCase("5", ForeignEnum.ForeignBar)]
  831. [TestCase("100", (ForeignEnum)100)]
  832. public void EnumValid(string value, ForeignEnum expectedValue)
  833. {
  834. string json = "{ \"singleForeignEnum\": " + value + " }";
  835. var parsed = TestAllTypes.Parser.ParseJson(json);
  836. Assert.AreEqual(new TestAllTypes { SingleForeignEnum = expectedValue }, parsed);
  837. }
  838. [Test]
  839. [TestCase("\"NOT_A_VALID_VALUE\"")]
  840. [TestCase("5.5")]
  841. public void Enum_Invalid(string value)
  842. {
  843. string json = "{ \"singleForeignEnum\": " + value + " }";
  844. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  845. }
  846. [Test]
  847. public void OneofDuplicate_Invalid()
  848. {
  849. string json = "{ \"oneofString\": \"x\", \"oneofUint32\": 10 }";
  850. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  851. }
  852. [Test]
  853. public void UnknownField_NotIgnored()
  854. {
  855. string json = "{ \"unknownField\": 10, \"singleString\": \"x\" }";
  856. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  857. }
  858. [Test]
  859. [TestCase("5")]
  860. [TestCase("\"text\"")]
  861. [TestCase("[0, 1, 2]")]
  862. [TestCase("{ \"a\": { \"b\": 10 } }")]
  863. public void UnknownField_Ignored(string value)
  864. {
  865. var parser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));
  866. string json = "{ \"unknownField\": " + value + ", \"singleString\": \"x\" }";
  867. var actual = parser.Parse<TestAllTypes>(json);
  868. var expected = new TestAllTypes { SingleString = "x" };
  869. Assert.AreEqual(expected, actual);
  870. }
  871. /// <summary>
  872. /// Various tests use strings which have quotes round them for parsing or as the result
  873. /// of formatting, but without those quotes being specified in the tests (for the sake of readability).
  874. /// This method simply returns the input, wrapped in double quotes.
  875. /// </summary>
  876. internal static string WrapInQuotes(string text)
  877. {
  878. return '"' + text + '"';
  879. }
  880. }
  881. }