PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 1647 lines | 1504 code | 124 blank | 19 comment | 11 complexity | 9ab72abac967c362318b98f93e0f67a3 MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using FluentAssertions;
  21. using MongoDB.Bson.IO;
  22. using MongoDB.Bson.Serialization;
  23. using MongoDB.Bson.TestHelpers;
  24. using MongoDB.Bson.TestHelpers.XunitExtensions;
  25. using Xunit;
  26. namespace MongoDB.Bson.Tests.IO
  27. {
  28. public class JsonReaderTests
  29. {
  30. private IBsonReader _bsonReader;
  31. [Theory]
  32. [ParameterAttributeData]
  33. public void JsonReader_should_support_reading_multiple_documents(
  34. [Range(0, 3)]
  35. int numberOfDocuments)
  36. {
  37. var document = new BsonDocument("x", 1);
  38. var json = document.ToJson();
  39. var input = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j);
  40. var expectedResult = Enumerable.Repeat(document, numberOfDocuments);
  41. using (var jsonReader = new JsonReader(input))
  42. {
  43. var result = new List<BsonDocument>();
  44. jsonReader.State.Should().Be(BsonReaderState.Initial);
  45. while (!jsonReader.IsAtEndOfFile())
  46. {
  47. jsonReader.ReadStartDocument();
  48. var name = jsonReader.ReadName();
  49. var value = jsonReader.ReadInt32();
  50. jsonReader.ReadEndDocument();
  51. jsonReader.State.Should().Be(BsonReaderState.Initial);
  52. var resultDocument = new BsonDocument(name, value);
  53. result.Add(resultDocument);
  54. }
  55. result.Should().Equal(expectedResult);
  56. }
  57. }
  58. [Fact]
  59. public void TestArrayEmpty()
  60. {
  61. var json = "[]";
  62. using (_bsonReader = new JsonReader(json))
  63. {
  64. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  65. _bsonReader.ReadStartArray();
  66. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  67. _bsonReader.ReadEndArray();
  68. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  69. Assert.True(_bsonReader.IsAtEndOfFile());
  70. }
  71. Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
  72. }
  73. [Fact]
  74. public void TestArrayOneElement()
  75. {
  76. var json = "[1]";
  77. using (_bsonReader = new JsonReader(json))
  78. {
  79. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  80. _bsonReader.ReadStartArray();
  81. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  82. Assert.Equal(1, _bsonReader.ReadInt32());
  83. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  84. _bsonReader.ReadEndArray();
  85. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  86. Assert.True(_bsonReader.IsAtEndOfFile());
  87. }
  88. Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
  89. }
  90. [Fact]
  91. public void TestArrayTwoElements()
  92. {
  93. var json = "[1, 2]";
  94. using (_bsonReader = new JsonReader(json))
  95. {
  96. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  97. _bsonReader.ReadStartArray();
  98. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  99. Assert.Equal(1, _bsonReader.ReadInt32());
  100. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  101. Assert.Equal(2, _bsonReader.ReadInt32());
  102. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  103. _bsonReader.ReadEndArray();
  104. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  105. Assert.True(_bsonReader.IsAtEndOfFile());
  106. }
  107. Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
  108. }
  109. [Theory]
  110. [InlineData("{ $binary : \"AQ==\", $type : 0 }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  111. [InlineData("{ $binary : \"AQ==\", $type : 128 }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  112. [InlineData("{ $binary : \"AQ==\", $type : \"0\" }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  113. [InlineData("{ $binary : \"AQ==\", $type : \"00\" }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  114. [InlineData("{ $binary : \"AQ==\", $type : \"80\" }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  115. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  116. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"00\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  117. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"80\" } }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  118. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  119. [InlineData("{ $binary : { subType : \"00\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  120. [InlineData("{ $binary : { subType : \"80\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  121. [InlineData("BinData(0, \"AQ==\")", new byte[] { 1 }, BsonBinarySubType.Binary)]
  122. [InlineData("BinData(128, \"AQ==\")", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  123. [InlineData("HexData(0, \"01\")", new byte[] { 1 }, BsonBinarySubType.Binary)]
  124. [InlineData("HexData(128, \"01\")", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  125. public void TestBinaryData(string json, byte[] expectedBytes, BsonBinarySubType expectedSubType)
  126. {
  127. using (var reader = new JsonReader(json))
  128. {
  129. var result = reader.ReadBinaryData();
  130. result.Should().Be(new BsonBinaryData(expectedBytes, expectedSubType));
  131. reader.State.Should().Be(BsonReaderState.Initial);
  132. reader.IsAtEndOfFile().Should().BeTrue();
  133. }
  134. }
  135. [Theory]
  136. [InlineData("{ $binary")]
  137. [InlineData("{ $binary :")]
  138. [InlineData("{ $binary : \"AQ==\"")]
  139. [InlineData("{ $binary : \"AQ==\",")]
  140. [InlineData("{ $binary : \"AQ==\", $type")]
  141. [InlineData("{ $binary : \"AQ==\", $type :")]
  142. [InlineData("{ $binary : \"AQ==\", $type : 0")]
  143. [InlineData("{ $binary : {")]
  144. [InlineData("{ $binary : { base64")]
  145. [InlineData("{ $binary : { base64 :")]
  146. [InlineData("{ $binary : { base64 : \"AQ==\"")]
  147. [InlineData("{ $binary : { base64 : \"AQ==\",")]
  148. [InlineData("{ $binary : { base64 : \"AQ==\", subType")]
  149. [InlineData("{ $binary : { base64 : \"AQ==\", subType :")]
  150. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\"")]
  151. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" }")]
  152. [InlineData("{ $binary : { subType")]
  153. [InlineData("{ $binary : { subType :")]
  154. [InlineData("{ $binary : { subType : \"0\"")]
  155. [InlineData("{ $binary : { subType : \"0\",")]
  156. [InlineData("{ $binary : { subType : \"0\", base64")]
  157. [InlineData("{ $binary : { subType : \"0\", base64 :")]
  158. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\"")]
  159. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" }")]
  160. [InlineData("BinData")]
  161. [InlineData("BinData(")]
  162. [InlineData("BinData(0")]
  163. [InlineData("BinData(0,")]
  164. [InlineData("BinData(0, \"AQ==\"")]
  165. [InlineData("HexData")]
  166. [InlineData("HexData(")]
  167. [InlineData("HexData(0")]
  168. [InlineData("HexData(0,")]
  169. [InlineData("HexData(0, \"01\"")]
  170. public void TestBinaryDataEndOfFile(string json)
  171. {
  172. using (var reader = new JsonReader(json))
  173. {
  174. var exception = Record.Exception(() => reader.ReadBinaryData());
  175. exception.Should().BeOfType<FormatException>();
  176. }
  177. }
  178. [Theory]
  179. [InlineData("{ $binary [")]
  180. [InlineData("{ $binary : [")]
  181. [InlineData("{ $binary : \"AQ==\" [")]
  182. [InlineData("{ $binary : \"AQ==\", [")]
  183. [InlineData("{ $binary : \"AQ==\", $type [")]
  184. [InlineData("{ $binary : \"AQ==\", $type : [")]
  185. [InlineData("{ $binary : \"AQ==\", $type : 0 [")]
  186. [InlineData("{ $binary : { [")]
  187. [InlineData("{ $binary : { base64 [")]
  188. [InlineData("{ $binary : { base64 : [")]
  189. [InlineData("{ $binary : { base64 : \"AQ==\" [")]
  190. [InlineData("{ $binary : { base64 : \"AQ==\", [")]
  191. [InlineData("{ $binary : { base64 : \"AQ==\", subType [")]
  192. [InlineData("{ $binary : { base64 : \"AQ==\", subType : [")]
  193. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"\" [")]
  194. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" [")]
  195. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" } [")]
  196. [InlineData("{ $binary : { subType [")]
  197. [InlineData("{ $binary : { subType : [")]
  198. [InlineData("{ $binary : { subType : \"\" [")]
  199. [InlineData("{ $binary : { subType : \"0\" [")]
  200. [InlineData("{ $binary : { subType : \"0\", [")]
  201. [InlineData("{ $binary : { subType : \"0\", base64 [")]
  202. [InlineData("{ $binary : { subType : \"0\", base64 : [")]
  203. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" [")]
  204. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" } [")]
  205. [InlineData("BinData [")]
  206. [InlineData("BinData( [")]
  207. [InlineData("BinData(0 [")]
  208. [InlineData("BinData(0, [")]
  209. [InlineData("BinData(0, \"AQ==\" [")]
  210. [InlineData("HexData [")]
  211. [InlineData("HexData( [")]
  212. [InlineData("HexData(0 [")]
  213. [InlineData("HexData(0, [")]
  214. [InlineData("HexData(0, \"01\" [")]
  215. public void TestBinaryDataInvalidToken(string json)
  216. {
  217. using (var reader = new JsonReader(json))
  218. {
  219. var exception = Record.Exception(() => reader.ReadBinaryData());
  220. exception.Should().BeOfType<FormatException>();
  221. }
  222. }
  223. [Fact]
  224. public void TestBookmark()
  225. {
  226. var json = "{ \"x\" : 1, \"y\" : 2 }";
  227. using (_bsonReader = new JsonReader(json))
  228. {
  229. // do everything twice returning to bookmark in between
  230. var bookmark = _bsonReader.GetBookmark();
  231. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  232. _bsonReader.ReturnToBookmark(bookmark);
  233. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  234. bookmark = _bsonReader.GetBookmark();
  235. _bsonReader.ReadStartDocument();
  236. _bsonReader.ReturnToBookmark(bookmark);
  237. _bsonReader.ReadStartDocument();
  238. bookmark = _bsonReader.GetBookmark();
  239. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  240. _bsonReader.ReturnToBookmark(bookmark);
  241. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  242. bookmark = _bsonReader.GetBookmark();
  243. Assert.Equal("x", _bsonReader.ReadName());
  244. _bsonReader.ReturnToBookmark(bookmark);
  245. Assert.Equal("x", _bsonReader.ReadName());
  246. bookmark = _bsonReader.GetBookmark();
  247. Assert.Equal(1, _bsonReader.ReadInt32());
  248. _bsonReader.ReturnToBookmark(bookmark);
  249. Assert.Equal(1, _bsonReader.ReadInt32());
  250. bookmark = _bsonReader.GetBookmark();
  251. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  252. _bsonReader.ReturnToBookmark(bookmark);
  253. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  254. bookmark = _bsonReader.GetBookmark();
  255. Assert.Equal("y", _bsonReader.ReadName());
  256. _bsonReader.ReturnToBookmark(bookmark);
  257. Assert.Equal("y", _bsonReader.ReadName());
  258. bookmark = _bsonReader.GetBookmark();
  259. Assert.Equal(2, _bsonReader.ReadInt32());
  260. _bsonReader.ReturnToBookmark(bookmark);
  261. Assert.Equal(2, _bsonReader.ReadInt32());
  262. bookmark = _bsonReader.GetBookmark();
  263. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  264. _bsonReader.ReturnToBookmark(bookmark);
  265. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  266. bookmark = _bsonReader.GetBookmark();
  267. _bsonReader.ReadEndDocument();
  268. _bsonReader.ReturnToBookmark(bookmark);
  269. _bsonReader.ReadEndDocument();
  270. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  271. Assert.True(_bsonReader.IsAtEndOfFile());
  272. }
  273. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  274. }
  275. [Fact]
  276. public void TestBooleanFalse()
  277. {
  278. var json = "false";
  279. using (_bsonReader = new JsonReader(json))
  280. {
  281. Assert.Equal(BsonType.Boolean, _bsonReader.ReadBsonType());
  282. Assert.Equal(false, _bsonReader.ReadBoolean());
  283. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  284. Assert.True(_bsonReader.IsAtEndOfFile());
  285. }
  286. Assert.Equal(json, BsonSerializer.Deserialize<bool>(json).ToJson());
  287. }
  288. [Fact]
  289. public void TestBooleanTrue()
  290. {
  291. var json = "true";
  292. using (_bsonReader = new JsonReader(json))
  293. {
  294. Assert.Equal(BsonType.Boolean, _bsonReader.ReadBsonType());
  295. Assert.Equal(true, _bsonReader.ReadBoolean());
  296. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  297. Assert.True(_bsonReader.IsAtEndOfFile());
  298. }
  299. Assert.Equal(json, BsonSerializer.Deserialize<bool>(json).ToJson());
  300. }
  301. [Theory]
  302. [InlineData("{ $date : 0 }", 0L)]
  303. [InlineData("{ $date : -9223372036854775808 }", -9223372036854775808L)]
  304. [InlineData("{ $date : 9223372036854775807 }", 9223372036854775807L)]
  305. [InlineData("{ $date : { $numberLong : 0 } }", 0L)]
  306. [InlineData("{ $date : { $numberLong : -9223372036854775808 } }", -9223372036854775808L)]
  307. [InlineData("{ $date : { $numberLong : 9223372036854775807 } }", 9223372036854775807L)]
  308. [InlineData("{ $date : { $numberLong : \"0\" } }", 0L)]
  309. [InlineData("{ $date : { $numberLong : \"-9223372036854775808\" } }", -9223372036854775808L)]
  310. [InlineData("{ $date : { $numberLong : \"9223372036854775807\" } }", 9223372036854775807L)]
  311. [InlineData("{ $date : \"1970-01-01T00:00:00Z\" }", 0L)]
  312. [InlineData("{ $date : \"0001-01-01T00:00:00Z\" }", -62135596800000L)]
  313. [InlineData("{ $date : \"1970-01-01T00:00:00.000Z\" }", 0L)]
  314. [InlineData("{ $date : \"0001-01-01T00:00:00.000Z\" }", -62135596800000L)]
  315. [InlineData("{ $date : \"9999-12-31T23:59:59.999Z\" }", 253402300799999L)]
  316. [InlineData("new Date(0)", 0L)]
  317. [InlineData("new Date(9223372036854775807)", 9223372036854775807L)]
  318. [InlineData("new Date(-9223372036854775808)", -9223372036854775808L)]
  319. [InlineData("ISODate(\"1970-01-01T00:00:00Z\")", 0L)]
  320. [InlineData("ISODate(\"0001-01-01T00:00:00Z\")", -62135596800000L)]
  321. [InlineData("ISODate(\"1970-01-01T00:00:00.000Z\")", 0L)]
  322. [InlineData("ISODate(\"0001-01-01T00:00:00.000Z\")", -62135596800000L)]
  323. [InlineData("ISODate(\"9999-12-31T23:59:59.999Z\")", 253402300799999L)]
  324. public void TestDateTime(string json, long expectedResult)
  325. {
  326. using (var reader = new JsonReader(json))
  327. {
  328. var result = reader.ReadDateTime();
  329. result.Should().Be(expectedResult);
  330. reader.State.Should().Be(BsonReaderState.Initial);
  331. reader.IsAtEndOfFile().Should().BeTrue();
  332. }
  333. }
  334. [Fact]
  335. public void TestDateTimeMinBson()
  336. {
  337. var json = "new Date(-9223372036854775808)";
  338. using (_bsonReader = new JsonReader(json))
  339. {
  340. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  341. Assert.Equal(-9223372036854775808, _bsonReader.ReadDateTime());
  342. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  343. Assert.True(_bsonReader.IsAtEndOfFile());
  344. }
  345. Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
  346. }
  347. [Fact]
  348. public void TestDateTimeMaxBson()
  349. {
  350. var json = "new Date(9223372036854775807)";
  351. using (_bsonReader = new JsonReader(json))
  352. {
  353. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  354. Assert.Equal(9223372036854775807, _bsonReader.ReadDateTime());
  355. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  356. Assert.True(_bsonReader.IsAtEndOfFile());
  357. }
  358. Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
  359. }
  360. [Theory]
  361. [InlineData("ISODate(\"1970\")", 0L, "ISODate(\"1970-01-01T00:00:00Z\")")]
  362. [InlineData("ISODate(\"1970-01\")", 0L, "ISODate(\"1970-01-01T00:00:00Z\")")]
  363. [InlineData("ISODate(\"1970-01-02\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  364. [InlineData("ISODate(\"1970-01-02T00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  365. [InlineData("ISODate(\"1970-01-02T00:01\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  366. [InlineData("ISODate(\"1970-01-02T00:01:02\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  367. [InlineData("ISODate(\"1970-01-02T00:01:02.003\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  368. [InlineData("ISODate(\"1970-01-02T00Z\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  369. [InlineData("ISODate(\"1970-01-02T00:01Z\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  370. [InlineData("ISODate(\"1970-01-02T00:01:02Z\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  371. [InlineData("ISODate(\"1970-01-02T00:01:02.003Z\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  372. [InlineData("ISODate(\"1970-01-02T00+00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  373. [InlineData("ISODate(\"1970-01-02T00:01+00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  374. [InlineData("ISODate(\"1970-01-02T00:01:02+00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  375. [InlineData("ISODate(\"1970-01-02T00:01:02.003+00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  376. [InlineData("ISODate(\"1970-01-02T00+00:00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  377. [InlineData("ISODate(\"1970-01-02T00:01+00:00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  378. [InlineData("ISODate(\"1970-01-02T00:01:02+00:00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  379. [InlineData("ISODate(\"1970-01-02T00:01:02.003+00:00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  380. [InlineData("ISODate(\"19700102\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  381. [InlineData("ISODate(\"19700102T00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  382. [InlineData("ISODate(\"19700102T0001\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  383. [InlineData("ISODate(\"19700102T000102\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  384. [InlineData("ISODate(\"19700102T000102.003\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  385. [InlineData("ISODate(\"19700102T00Z\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  386. [InlineData("ISODate(\"19700102T0001Z\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  387. [InlineData("ISODate(\"19700102T000102Z\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  388. [InlineData("ISODate(\"19700102T000102.003Z\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  389. [InlineData("ISODate(\"19700102T00+00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  390. [InlineData("ISODate(\"19700102T0001+00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  391. [InlineData("ISODate(\"19700102T000102+00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  392. [InlineData("ISODate(\"19700102T000102.003+00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  393. [InlineData("ISODate(\"19700102T00+00:00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  394. [InlineData("ISODate(\"19700102T0001+00:00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  395. [InlineData("ISODate(\"19700102T000102+00:00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  396. [InlineData("ISODate(\"19700102T000102.003+00:00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  397. public void TestDateTimeShell(string json, long expectedResult, string canonicalJson)
  398. {
  399. using (_bsonReader = new JsonReader(json))
  400. {
  401. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  402. Assert.Equal(expectedResult, _bsonReader.ReadDateTime());
  403. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  404. Assert.True(_bsonReader.IsAtEndOfFile());
  405. }
  406. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell };
  407. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
  408. }
  409. [Fact]
  410. public void TestDateTimeStrict()
  411. {
  412. var json = "{ \"$date\" : 0 }";
  413. using (_bsonReader = new JsonReader(json))
  414. {
  415. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  416. Assert.Equal(0, _bsonReader.ReadDateTime());
  417. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  418. Assert.True(_bsonReader.IsAtEndOfFile());
  419. }
  420. #pragma warning disable 618
  421. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  422. #pragma warning restore 618
  423. Assert.Equal(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
  424. }
  425. [Fact]
  426. public void TestDateTimeStrictIso8601()
  427. {
  428. var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }";
  429. using (_bsonReader = new JsonReader(json))
  430. {
  431. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  432. Assert.Equal(0, _bsonReader.ReadDateTime());
  433. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  434. Assert.True(_bsonReader.IsAtEndOfFile());
  435. }
  436. var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out
  437. #pragma warning disable 618
  438. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  439. #pragma warning restore 618
  440. Assert.Equal(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
  441. }
  442. [Theory]
  443. [InlineData("{ $date: { \"$numberLong\": \"1552949630483\" } }", 1552949630483L)]
  444. [InlineData("{ $date: { $numberLong: \"1552949630483\" } }", 1552949630483L)]
  445. public void TestDateTimeWithNumberLong(string json, long expectedResult)
  446. {
  447. using (var reader = new JsonReader(json))
  448. {
  449. var result = reader.ReadDateTime();
  450. result.Should().Be(expectedResult);
  451. reader.State.Should().Be(BsonReaderState.Initial);
  452. reader.IsAtEndOfFile().Should().BeTrue();
  453. }
  454. }
  455. [Theory]
  456. [InlineData("NumberDecimal(1)", "1", "NumberDecimal(\"1\")")]
  457. [InlineData("NumberDecimal(2147483648)", "2147483648", "NumberDecimal(\"2147483648\")")]
  458. [InlineData("NumberDecimal(\"1.5\")", "1.5", "NumberDecimal(\"1.5\")")]
  459. public void TestDecimal128Constructor(string json, string expectedValueString, string expectedJson)
  460. {
  461. using (_bsonReader = new JsonReader(json))
  462. {
  463. Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType());
  464. Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128());
  465. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  466. Assert.True(_bsonReader.IsAtEndOfFile());
  467. }
  468. Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson());
  469. }
  470. [Theory]
  471. [InlineData("{ $numberDecimal : 1 }", "1", "NumberDecimal(\"1\")")]
  472. [InlineData("{ $numberDecimal : 2147483648 }", "2147483648", "NumberDecimal(\"2147483648\")")]
  473. [InlineData("{ $numberDecimal : \"1.5\" }", "1.5", "NumberDecimal(\"1.5\")")]
  474. [InlineData("{ $numberDecimal : \"Infinity\" }", "Infinity", "NumberDecimal(\"Infinity\")")]
  475. [InlineData("{ $numberDecimal : \"-Infinity\" }", "-Infinity", "NumberDecimal(\"-Infinity\")")]
  476. [InlineData("{ $numberDecimal : \"NaN\" }", "NaN", "NumberDecimal(\"NaN\")")]
  477. public void TestDecimal128ExtendedJson(string json, string expectedValueString, string expectedJson)
  478. {
  479. using (_bsonReader = new JsonReader(json))
  480. {
  481. Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType());
  482. Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128());
  483. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  484. Assert.True(_bsonReader.IsAtEndOfFile());
  485. }
  486. Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson());
  487. }
  488. [Fact]
  489. public void TestDocumentEmpty()
  490. {
  491. var json = "{ }";
  492. using (_bsonReader = new JsonReader(json))
  493. {
  494. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  495. _bsonReader.ReadStartDocument();
  496. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  497. _bsonReader.ReadEndDocument();
  498. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  499. Assert.True(_bsonReader.IsAtEndOfFile());
  500. }
  501. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  502. }
  503. [Fact]
  504. public void TestDocumentNested()
  505. {
  506. var json = "{ \"a\" : { \"x\" : 1 }, \"y\" : 2 }";
  507. using (_bsonReader = new JsonReader(json))
  508. {
  509. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  510. _bsonReader.ReadStartDocument();
  511. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  512. Assert.Equal("a", _bsonReader.ReadName());
  513. _bsonReader.ReadStartDocument();
  514. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  515. Assert.Equal("x", _bsonReader.ReadName());
  516. Assert.Equal(1, _bsonReader.ReadInt32());
  517. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  518. _bsonReader.ReadEndDocument();
  519. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  520. Assert.Equal("y", _bsonReader.ReadName());
  521. Assert.Equal(2, _bsonReader.ReadInt32());
  522. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  523. _bsonReader.ReadEndDocument();
  524. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  525. Assert.True(_bsonReader.IsAtEndOfFile());
  526. }
  527. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  528. }
  529. [Fact]
  530. public void TestDocumentOneElement()
  531. {
  532. var json = "{ \"x\" : 1 }";
  533. using (_bsonReader = new JsonReader(json))
  534. {
  535. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  536. _bsonReader.ReadStartDocument();
  537. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  538. Assert.Equal("x", _bsonReader.ReadName());
  539. Assert.Equal(1, _bsonReader.ReadInt32());
  540. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  541. _bsonReader.ReadEndDocument();
  542. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  543. Assert.True(_bsonReader.IsAtEndOfFile());
  544. }
  545. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  546. }
  547. [Fact]
  548. public void TestDocumentTwoElements()
  549. {
  550. var json = "{ \"x\" : 1, \"y\" : 2 }";
  551. using (_bsonReader = new JsonReader(json))
  552. {
  553. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  554. _bsonReader.ReadStartDocument();
  555. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  556. Assert.Equal("x", _bsonReader.ReadName());
  557. Assert.Equal(1, _bsonReader.ReadInt32());
  558. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  559. Assert.Equal("y", _bsonReader.ReadName());
  560. Assert.Equal(2, _bsonReader.ReadInt32());
  561. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  562. _bsonReader.ReadEndDocument();
  563. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  564. Assert.True(_bsonReader.IsAtEndOfFile());
  565. }
  566. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  567. }
  568. [Theory]
  569. [InlineData("1.0", 1.0)]
  570. [InlineData("-1.0", -1.0)]
  571. [InlineData("1.5", 1.5)]
  572. [InlineData("{ $numberDouble : 1 }", 1.0)]
  573. [InlineData("{ $numberDouble : 1.0 }", 1.0)]
  574. [InlineData("{ $numberDouble : \"1\" }", 1.0)]
  575. [InlineData("{ $numberDouble : \"-1\" }", -1.0)]
  576. [InlineData("{ $numberDouble : \"1.5\" }", 1.5)]
  577. [InlineData("{ $numberDouble : \"Infinity\" }", double.PositiveInfinity)]
  578. [InlineData("{ $numberDouble : \"-Infinity\" }", double.NegativeInfinity)]
  579. [InlineData("{ $numberDouble : \"NaN\" }", double.NaN)]
  580. public void TestDouble(string json, double expectedValue)
  581. {
  582. using (var reader = new JsonReader(json))
  583. {
  584. reader.ReadBsonType().Should().Be(BsonType.Double);
  585. reader.ReadDouble().Should().Be(expectedValue);
  586. reader.State.Should().Be(BsonReaderState.Initial);
  587. reader.IsAtEndOfFile().Should().BeTrue();
  588. }
  589. }
  590. [Theory]
  591. [InlineData("{ $numberDouble")]
  592. [InlineData("{ $numberDouble :")]
  593. [InlineData("{ $numberDouble : \"1\"")]
  594. public void TestDoubleEndOfFile(string json)
  595. {
  596. using (var reader = new JsonReader(json))
  597. {
  598. var exception = Record.Exception(() => reader.ReadDouble());
  599. exception.Should().BeOfType<FormatException>();
  600. }
  601. }
  602. [Theory]
  603. [InlineData("{ $numberDouble [")]
  604. [InlineData("{ $numberDouble : [")]
  605. [InlineData("{ $numberDouble : \"1\" [")]
  606. public void TestDoubleInvalidToken(string json)
  607. {
  608. using (var reader = new JsonReader(json))
  609. {
  610. var exception = Record.Exception(() => reader.ReadDouble());
  611. exception.Should().BeOfType<FormatException>();
  612. }
  613. }
  614. [Fact]
  615. public void TestDoubleRoundTrip()
  616. {
  617. var json = "1.5";
  618. using (_bsonReader = new JsonReader(json))
  619. {
  620. Assert.Equal(BsonType.Double, _bsonReader.ReadBsonType());
  621. Assert.Equal(1.5, _bsonReader.ReadDouble());
  622. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  623. Assert.True(_bsonReader.IsAtEndOfFile());
  624. }
  625. Assert.Equal(json, BsonSerializer.Deserialize<double>(json).ToJson());
  626. }
  627. [Theory]
  628. [ParameterAttributeData]
  629. [ResetGuidModeAfterTest]
  630. public void TestGuid(
  631. [ClassValues(typeof(GuidModeValues))] GuidMode mode)
  632. {
  633. mode.Set();
  634. #pragma warning disable 618
  635. var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6");
  636. var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")";
  637. using (_bsonReader = new JsonReader(json))
  638. {
  639. Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
  640. var binaryData = _bsonReader.ReadBinaryData();
  641. Assert.True(binaryData.Bytes.SequenceEqual(guid.ToByteArray()));
  642. Assert.Equal(BsonBinarySubType.UuidLegacy, binaryData.SubType);
  643. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  644. {
  645. Assert.Equal(GuidRepresentation.CSharpLegacy, binaryData.GuidRepresentation);
  646. }
  647. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  648. Assert.True(_bsonReader.IsAtEndOfFile());
  649. }
  650. var guidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified;
  651. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && guidRepresentation != GuidRepresentation.Unspecified)
  652. {
  653. string expected;
  654. switch (guidRepresentation)
  655. {
  656. case GuidRepresentation.CSharpLegacy: expected = "CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  657. case GuidRepresentation.JavaLegacy: expected = "JUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  658. case GuidRepresentation.PythonLegacy: expected = "PYUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  659. case GuidRepresentation.Standard: expected = "UUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  660. default: throw new Exception("Unexpected GuidRepresentation.");
  661. }
  662. Assert.Equal(expected, BsonSerializer.Deserialize<Guid>(json).ToJson(new JsonWriterSettings()));
  663. }
  664. else
  665. {
  666. var exception = Record.Exception(() => guid.ToJson(new JsonWriterSettings()));
  667. exception.Should().BeOfType<BsonSerializationException>();
  668. }
  669. #pragma warning restore 618
  670. }
  671. [Fact]
  672. public void TestHexData()
  673. {
  674. var expectedBytes = new byte[] { 0x01, 0x23 };
  675. var json = "HexData(0, \"123\")";
  676. using (_bsonReader = new JsonReader(json))
  677. {
  678. Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
  679. var bytes = _bsonReader.ReadBytes();
  680. Assert.True(expectedBytes.SequenceEqual(bytes));
  681. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  682. Assert.True(_bsonReader.IsAtEndOfFile());
  683. }
  684. var expectedJson = "new BinData(0, \"ASM=\")";
  685. Assert.Equal(expectedJson, BsonSerializer.Deserialize<byte[]>(json).ToJson());
  686. }
  687. [Fact]
  688. public void TestInt32()
  689. {
  690. var json = "123";
  691. using (_bsonReader = new JsonReader(json))
  692. {
  693. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  694. Assert.Equal(123, _bsonReader.ReadInt32());
  695. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  696. Assert.True(_bsonReader.IsAtEndOfFile());
  697. }
  698. Assert.Equal(json, BsonSerializer.Deserialize<int>(json).ToJson());
  699. }
  700. [Theory]
  701. [InlineData("{ $numberInt : 1 }", 1)]
  702. [InlineData("{ $numberInt : -2147483648 }", -2147483648)]
  703. [InlineData("{ $numberInt : 2147483647 }", 2147483647)]
  704. [InlineData("{ $numberInt : \"1\" }", 1)]
  705. [InlineData("{ $numberInt : \"-2147483648\" }", -2147483648)]
  706. [InlineData("{ $numberInt : \"2147483647\" }", 2147483647)]
  707. public void TestInt32ExtendedJson(string json, int expectedResult)
  708. {
  709. using (var reader = new JsonReader(json))
  710. {
  711. var result = reader.ReadInt32();
  712. result.Should().Be(expectedResult);
  713. reader.State.Should().Be(BsonReaderState.Initial);
  714. reader.IsAtEndOfFile().Should().BeTrue();
  715. }
  716. }
  717. [Theory]
  718. // truncated input
  719. [InlineData("{ $numberInt")]
  720. [InlineData("{ $numberInt :")]
  721. [InlineData("{ $numberInt : 1")]
  722. // invalid extended json
  723. [InlineData("{ $numberInt ,")]
  724. [InlineData("{ $numberInt : \"abc\"")]
  725. [InlineData("{ $numberInt : 1,")]
  726. public void TestInt32ExtendedJsonInvalid(string json)
  727. {
  728. using (var reader = new JsonReader(json))
  729. {
  730. var execption = Record.Exception(() => reader.ReadInt32());
  731. execption.Should().BeOfType<FormatException>();
  732. }
  733. }
  734. [Theory]
  735. [InlineData("Number(123)")]
  736. [InlineData("NumberInt(123)")]
  737. public void TestInt32Constructor(string json)
  738. {
  739. using (_bsonReader = new JsonReader(json))
  740. {
  741. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  742. Assert.Equal(123, _bsonReader.ReadInt32());
  743. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  744. Assert.True(_bsonReader.IsAtEndOfFile());
  745. }
  746. var canonicalJson = "123";
  747. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<int>(new StringReader(json)).ToJson());
  748. }
  749. [Theory]
  750. [InlineData("{ $numberLong: 1 }", 1L)]
  751. [InlineData("{ $numberLong: -9223372036854775808 }", -9223372036854775808L)]
  752. [InlineData("{ $numberLong: 9223372036854775807 }", 9223372036854775807L)]
  753. [InlineData("{ $numberLong: \"1\" }", 1L)]
  754. [InlineData("{ $numberLong: \"-9223372036854775808\" }", -9223372036854775808L)]
  755. [InlineData("{ $numberLong: \"9223372036854775807\" }", 9223372036854775807L)]
  756. [InlineData("NumberLong(1)", 1L)]
  757. [InlineData("NumberLong(-9223372036854775808)", -9223372036854775808L)]
  758. [InlineData("NumberLong(9223372036854775807)", 9223372036854775807L)]
  759. [InlineData("NumberLong(\"1\")", 1L)]
  760. [InlineData("NumberLong(\"-9223372036854775808\")", -9223372036854775808L)]
  761. [InlineData("NumberLong(\"9223372036854775807\")", 9223372036854775807L)]
  762. public void TestInt64(string json, long expectedResult)
  763. {
  764. using (var reader = new JsonReader(json))
  765. {
  766. var result = reader.ReadInt64();
  767. result.Should().Be(expectedResult);
  768. reader.State.Should().Be(BsonReaderState.Initial);
  769. reader.IsAtEndOfFile().Should().BeTrue();
  770. }
  771. }
  772. [Fact]
  773. public void TestInt64ConstructorQuoted()
  774. {
  775. var json = "NumberLong(\"123456789012\")";
  776. using (_bsonReader = new JsonReader(json))
  777. {
  778. Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
  779. Assert.Equal(123456789012, _bsonReader.ReadInt64());
  780. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  781. Assert.True(_bsonReader.IsAtEndOfFile());
  782. }
  783. Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson());
  784. }
  785. [Fact]
  786. public void TestInt64ConstructorUnqutoed()
  787. {
  788. var json = "NumberLong(123)";
  789. using (_bsonReader = new JsonReader(json))
  790. {
  791. Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
  792. Assert.Equal(123, _bsonReader.ReadInt64());
  793. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  794. Assert.True(_bsonReader.IsAtEndOfFile());
  795. }
  796. Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson());
  797. }
  798. [Fact]
  799. public void TestIsAtEndOfFileWithTwoArrays()
  800. {
  801. var json = "[1,2][1,2]";
  802. using (var jsonReader = new JsonReader(json))
  803. {
  804. var count = 0;
  805. jsonReader.State.Should().Be(BsonReaderState.Initial);
  806. while (!jsonReader.IsAtEndOfFile())
  807. {
  808. var array = BsonSerializer.Deserialize<BsonArray>(jsonReader);
  809. jsonReader.State.Should().Be(BsonReaderState.Initial);
  810. var expected = new BsonArray { 1, 2 };
  811. Assert.Equal(expected, array);
  812. count += 1;
  813. }
  814. Assert.Equal(2, count);
  815. }
  816. }
  817. [Fact]
  818. public void TestIsAtEndOfFileWithTwoDocuments()
  819. {
  820. var json = "{x:1}{x:1}";
  821. using (var jsonReader = new JsonReader(json))
  822. {
  823. var count = 0;
  824. jsonReader.State.Should().Be(BsonReaderState.Initial);
  825. while (!jsonReader.IsAtEndOfFile())
  826. {
  827. var document = BsonSerializer.Deserialize<BsonDocument>(jsonReader);
  828. jsonReader.State.Should().Be(BsonReaderState.Initial);
  829. var expected = new BsonDocument("x", 1);
  830. Assert.Equal(expected, document);
  831. count += 1;
  832. }
  833. Assert.Equal(2, count);
  834. }
  835. }
  836. [Fact]
  837. public void TestInt64ExtendedJson()
  838. {
  839. var json = "{ \"$numberLong\" : \"123\" }";
  840. using (_bsonReader = new JsonReader(json))
  841. {
  842. Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
  843. Assert.Equal(123, _bsonReader.ReadInt64());
  844. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  845. Assert.True(_bsonReader.IsAtEndOfFile());
  846. }
  847. var canonicalJson = "NumberLong(123)";
  848. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<long>(new StringReader(json)).ToJson());
  849. }
  850. [Fact]
  851. public void TestJavaScript()
  852. {
  853. string json = "{ \"$code\" : \"function f() { return 1; }\" }";
  854. using (_bsonReader = new JsonReader(json))
  855. {
  856. Assert.Equal(BsonType.JavaScript, _bsonReader.ReadBsonType());
  857. Assert.Equal("function f() { return 1; }", _bsonReader.ReadJavaScript());
  858. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  859. Assert.True(_bsonReader.IsAtEndOfFile());
  860. }
  861. Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScript>(json).ToJson());
  862. }
  863. [Fact]
  864. public void TestJavaScriptWithScope()
  865. {
  866. string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }";
  867. using (_bsonReader = new JsonReader(json))
  868. {
  869. Assert.Equal(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType());
  870. Assert.Equal("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope());
  871. _bsonReader.ReadStartDocument();
  872. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  873. Assert.Equal("n", _bsonReader.ReadName());
  874. Assert.Equal(1, _bsonReader.ReadInt32());
  875. _bsonReader.ReadEndDocument();
  876. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  877. Assert.True(_bsonReader.IsAtEndOfFile());
  878. }
  879. Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScriptWithScope>(json).ToJson());
  880. }
  881. [Theory]
  882. [InlineData("{ $maxKey : 1 }")]
  883. [InlineData("MaxKey")]
  884. public void TestMaxKey(string json)
  885. {
  886. using (var reader = new JsonReader(json))
  887. {
  888. reader.ReadMaxKey();
  889. reader.State.Should().Be(BsonReaderState.Initial);
  890. reader.IsAtEndOfFile().Should().BeTrue();
  891. }
  892. }
  893. [Fact]
  894. public void TestMaxKeyExtendedJson()
  895. {
  896. var json = "{ \"$maxkey\" : 1 }";
  897. using (_bsonReader = new JsonReader(json))
  898. {
  899. Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
  900. _bsonReader.ReadMaxKey();
  901. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  902. Assert.True(_bsonReader.IsAtEndOfFile());
  903. }
  904. var canonicalJson = "MaxKey";
  905. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
  906. }
  907. [Fact]
  908. public void TestMaxKeyExtendedJsonWithCapitalK()
  909. {
  910. var json = "{ \"$maxKey\" : 1 }";
  911. using (_bsonReader = new JsonReader(json))
  912. {
  913. Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
  914. _bsonReader.ReadMaxKey();
  915. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  916. Assert.True(_bsonReader.IsAtEndOfFile());
  917. }
  918. var canonicalJson = "MaxKey";
  919. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
  920. }
  921. [Fact]
  922. public void TestMaxKeyKeyword()
  923. {
  924. var json = "MaxKey";
  925. using (_bsonReader = new JsonReader(json))
  926. {
  927. Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
  928. _bsonReader.ReadMaxKey();
  929. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  930. Assert.True(_bsonReader.IsAtEndOfFile());
  931. }
  932. Assert.Equal(json, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
  933. }
  934. [Theory]
  935. [InlineData("{ $minKey : 1 }")]
  936. [InlineData("MinKey")]
  937. public void TestMinKey(string json)
  938. {
  939. using (var reader = new JsonReader(json))
  940. {
  941. reader.ReadMinKey();
  942. reader.State.Should().Be(BsonReaderState.Initial);
  943. reader.IsAtEndOfFile().Should().BeTrue();
  944. }
  945. }
  946. [Fact]
  947. public void TestMinKeyExtendedJson()
  948. {
  949. var json = "{ \"$minkey\" : 1 }";
  950. using (_bsonReader = new JsonReader(json))
  951. {
  952. Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
  953. _bsonReader.ReadMinKey();
  954. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  955. Assert.True(_bsonReader.IsAtEndOfFile());
  956. }
  957. var canonicalJson = "MinKey";
  958. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
  959. }
  960. [Fact]
  961. public void TestMinKeyExtendedJsonWithCapitalK()
  962. {
  963. var json = "{ \"$minKey\" : 1 }";
  964. using (_bsonReader = new JsonReader(json))
  965. {
  966. Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
  967. _bsonReader.ReadMinKey();
  968. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  969. Assert.True(_bsonReader.IsAtEndOfFile());
  970. }
  971. var canonicalJson = "MinKey";
  972. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
  973. }
  974. [Fact]
  975. public void TestMinKeyKeyword()
  976. {
  977. var json = "MinKey";
  978. using (_bsonReader = new JsonReader(json))
  979. {
  980. Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
  981. _bsonReader.ReadMinKey();
  982. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  983. Assert.True(_bsonReader.IsAtEndOfFile());
  984. }
  985. Assert.Equal(json, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
  986. }
  987. [Fact]
  988. public void TestNestedArray()
  989. {
  990. var json = "{ \"a\" : [1, 2] }";
  991. using (_bsonReader = new JsonReader(json))
  992. {
  993. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  994. _bsonReader.ReadStart

Large files files are truncated, but you can click here to view the full file