PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MongoDB.Bson.Tests/IO/JsonWriterTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 601 lines | 539 code | 48 blank | 14 comment | 1 complexity | 087b5a78de3ce71eae735d0ee662e975 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-2014 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.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.IO;
  21. using MongoDB.Bson.Serialization;
  22. using FluentAssertions;
  23. using NUnit.Framework;
  24. namespace MongoDB.Bson.Tests.IO
  25. {
  26. [TestFixture]
  27. public class JsonWriterTests
  28. {
  29. private class TestData<T>
  30. {
  31. public T Value;
  32. public string Expected;
  33. public TestData(T value, string expected)
  34. {
  35. this.Value = value;
  36. this.Expected = expected;
  37. }
  38. }
  39. [Test]
  40. public void JsonWriter_should_support_writing_multiple_documents(
  41. [Range(0, 3)]
  42. int numberOfDocuments,
  43. [Values("", " ", "\r\n")]
  44. string documentSeparator)
  45. {
  46. var document = new BsonDocument("x", 1);
  47. var json = document.ToJson();
  48. var expectedResult = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j + documentSeparator);
  49. using (var stringWriter = new StringWriter())
  50. using (var jsonWriter = new JsonWriter(stringWriter))
  51. {
  52. for (var n = 0; n < numberOfDocuments; n++)
  53. {
  54. jsonWriter.WriteStartDocument();
  55. jsonWriter.WriteName("x");
  56. jsonWriter.WriteInt32(1);
  57. jsonWriter.WriteEndDocument();
  58. jsonWriter.BaseTextWriter.Write(documentSeparator);
  59. }
  60. var result = stringWriter.ToString();
  61. result.Should().Be(expectedResult);
  62. }
  63. }
  64. [Test]
  65. public void TestEmptyDocument()
  66. {
  67. BsonDocument document = new BsonDocument();
  68. string json = document.ToJson();
  69. string expected = "{ }";
  70. Assert.AreEqual(expected, json);
  71. }
  72. [Test]
  73. public void TestSingleString()
  74. {
  75. BsonDocument document = new BsonDocument() { { "abc", "xyz" } };
  76. string json = document.ToJson();
  77. string expected = "{ \"abc\" : \"xyz\" }";
  78. Assert.AreEqual(expected, json);
  79. }
  80. [Test]
  81. public void TestIndentedEmptyDocument()
  82. {
  83. BsonDocument document = new BsonDocument();
  84. var settings = new JsonWriterSettings { Indent = true };
  85. string json = document.ToJson(settings);
  86. string expected = "{ }";
  87. Assert.AreEqual(expected, json);
  88. }
  89. [Test]
  90. public void TestIndentedOneElement()
  91. {
  92. BsonDocument document = new BsonDocument() { { "name", "value" } };
  93. var settings = new JsonWriterSettings { Indent = true };
  94. string json = document.ToJson(settings);
  95. string expected = "{\r\n \"name\" : \"value\"\r\n}";
  96. Assert.AreEqual(expected, json);
  97. }
  98. [Test]
  99. public void TestIndentedTwoElements()
  100. {
  101. BsonDocument document = new BsonDocument() { { "a", "x" }, { "b", "y" } };
  102. var settings = new JsonWriterSettings { Indent = true };
  103. string json = document.ToJson(settings);
  104. string expected = "{\r\n \"a\" : \"x\",\r\n \"b\" : \"y\"\r\n}";
  105. Assert.AreEqual(expected, json);
  106. }
  107. [Test]
  108. public void TestDouble()
  109. {
  110. var tests = new TestData<double>[]
  111. {
  112. new TestData<double>(0.0, "0.0"),
  113. new TestData<double>(0.0005, "0.0005"),
  114. new TestData<double>(0.5, "0.5"),
  115. new TestData<double>(1.0, "1.0"),
  116. new TestData<double>(1.5, "1.5"),
  117. new TestData<double>(1.5E+40, "1.5E+40"),
  118. new TestData<double>(1.5E-40, "1.5E-40"),
  119. new TestData<double>(1234567890.1234568E+123, "1.2345678901234568E+132"),
  120. new TestData<double>(double.Epsilon, "4.94065645841247E-324"),
  121. new TestData<double>(double.MaxValue, "1.7976931348623157E+308"),
  122. new TestData<double>(double.MinValue, "-1.7976931348623157E+308"),
  123. new TestData<double>(-0.0005, "-0.0005"),
  124. new TestData<double>(-0.5, "-0.5"),
  125. new TestData<double>(-1.0, "-1.0"),
  126. new TestData<double>(-1.5, "-1.5"),
  127. new TestData<double>(-1.5E+40, "-1.5E+40"),
  128. new TestData<double>(-1.5E-40, "-1.5E-40"),
  129. new TestData<double>(-1234567890.1234568E+123, "-1.2345678901234568E+132"),
  130. new TestData<double>(-double.Epsilon, "-4.94065645841247E-324"),
  131. new TestData<double>(double.NaN, "NaN"),
  132. new TestData<double>(double.NegativeInfinity, "-Infinity"),
  133. new TestData<double>(double.PositiveInfinity, "Infinity")
  134. };
  135. foreach (var test in tests)
  136. {
  137. var json = test.Value.ToJson();
  138. Assert.AreEqual(test.Expected, json);
  139. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<double>(json));
  140. }
  141. }
  142. [Test]
  143. public void TestInt64Shell()
  144. {
  145. var tests = new TestData<long>[]
  146. {
  147. new TestData<long>(long.MinValue, "NumberLong(\"-9223372036854775808\")"),
  148. new TestData<long>(int.MinValue - 1L, "NumberLong(\"-2147483649\")"),
  149. new TestData<long>(int.MinValue, "NumberLong(-2147483648)"),
  150. new TestData<long>(0, "NumberLong(0)"),
  151. new TestData<long>(int.MaxValue, "NumberLong(2147483647)"),
  152. new TestData<long>(int.MaxValue + 1L, "NumberLong(\"2147483648\")"),
  153. new TestData<long>(long.MaxValue, "NumberLong(\"9223372036854775807\")")
  154. };
  155. foreach (var test in tests)
  156. {
  157. var json = test.Value.ToJson();
  158. Assert.AreEqual(test.Expected, json);
  159. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<long>(json));
  160. }
  161. }
  162. [Test]
  163. public void TestInt64Strict()
  164. {
  165. var tests = new TestData<long>[]
  166. {
  167. new TestData<long>(long.MinValue, "-9223372036854775808"),
  168. new TestData<long>(int.MinValue - 1L, "-2147483649"),
  169. new TestData<long>(int.MinValue, "-2147483648"),
  170. new TestData<long>(0, "0"),
  171. new TestData<long>(int.MaxValue, "2147483647"),
  172. new TestData<long>(int.MaxValue + 1L, "2147483648"),
  173. new TestData<long>(long.MaxValue, "9223372036854775807")
  174. };
  175. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  176. foreach (var test in tests)
  177. {
  178. var json = test.Value.ToJson(jsonSettings);
  179. Assert.AreEqual(test.Expected, json);
  180. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<long>(json));
  181. }
  182. }
  183. [Test]
  184. public void TestEmbeddedDocument()
  185. {
  186. BsonDocument document = new BsonDocument
  187. {
  188. { "doc", new BsonDocument { { "a", 1 }, { "b", 2 } } }
  189. };
  190. string json = document.ToJson();
  191. string expected = "{ \"doc\" : { \"a\" : 1, \"b\" : 2 } }";
  192. Assert.AreEqual(expected, json);
  193. }
  194. [Test]
  195. public void TestIndentedEmbeddedDocument()
  196. {
  197. BsonDocument document = new BsonDocument
  198. {
  199. { "doc", new BsonDocument { { "a", 1 }, { "b", 2 } } }
  200. };
  201. var settings = new JsonWriterSettings { Indent = true };
  202. string json = document.ToJson(settings);
  203. string expected = "{\r\n \"doc\" : {\r\n \"a\" : 1,\r\n \"b\" : 2\r\n }\r\n}";
  204. Assert.AreEqual(expected, json);
  205. }
  206. [Test]
  207. public void TestArray()
  208. {
  209. BsonDocument document = new BsonDocument
  210. {
  211. { "array", new BsonArray { 1, 2, 3 } }
  212. };
  213. string json = document.ToJson();
  214. string expected = "{ \"array\" : [1, 2, 3] }";
  215. Assert.AreEqual(expected, json);
  216. }
  217. [Test]
  218. public void TestBinaryShell()
  219. {
  220. var tests = new TestData<BsonBinaryData>[]
  221. {
  222. new TestData<BsonBinaryData>(new byte[] { }, "new BinData(0, \"\")"),
  223. new TestData<BsonBinaryData>(new byte[] { 1 }, "new BinData(0, \"AQ==\")"),
  224. new TestData<BsonBinaryData>(new byte[] { 1, 2 }, "new BinData(0, \"AQI=\")"),
  225. new TestData<BsonBinaryData>(new byte[] { 1, 2, 3 }, "new BinData(0, \"AQID\")"),
  226. new TestData<BsonBinaryData>(Guid.Empty, "CSUUID(\"00000000-0000-0000-0000-000000000000\")")
  227. };
  228. foreach (var test in tests)
  229. {
  230. var json = test.Value.ToJson();
  231. Assert.AreEqual(test.Expected, json);
  232. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<BsonBinaryData>(json));
  233. }
  234. }
  235. [Test]
  236. public void TestBinaryStrict()
  237. {
  238. var tests = new TestData<BsonBinaryData>[]
  239. {
  240. new TestData<BsonBinaryData>(new byte[] { }, "{ \"$binary\" : \"\", \"$type\" : \"00\" }"),
  241. new TestData<BsonBinaryData>(new byte[] { 1 }, "{ \"$binary\" : \"AQ==\", \"$type\" : \"00\" }"),
  242. new TestData<BsonBinaryData>(new byte[] { 1, 2 }, "{ \"$binary\" : \"AQI=\", \"$type\" : \"00\" }"),
  243. new TestData<BsonBinaryData>(new byte[] { 1, 2, 3 }, "{ \"$binary\" : \"AQID\", \"$type\" : \"00\" }"),
  244. new TestData<BsonBinaryData>(Guid.Empty, "{ \"$binary\" : \"AAAAAAAAAAAAAAAAAAAAAA==\", \"$type\" : \"03\" }")
  245. };
  246. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  247. foreach (var test in tests)
  248. {
  249. var json = test.Value.ToJson(jsonSettings);
  250. Assert.AreEqual(test.Expected, json);
  251. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<BsonBinaryData>(json));
  252. }
  253. }
  254. [Test]
  255. public void TestDateTimeShell()
  256. {
  257. var utcNow = DateTime.UtcNow;
  258. var utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000));
  259. var isoDate = string.Format("ISODate(\"{0}\")", utcNowTruncated.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ"));
  260. var tests = new TestData<BsonDateTime>[]
  261. {
  262. new TestData<BsonDateTime>(new BsonDateTime(long.MinValue), "new Date(-9223372036854775808)"),
  263. new TestData<BsonDateTime>(new BsonDateTime(0), "ISODate(\"1970-01-01T00:00:00Z\")"),
  264. new TestData<BsonDateTime>(new BsonDateTime(long.MaxValue), "new Date(9223372036854775807)"),
  265. new TestData<BsonDateTime>(new BsonDateTime(DateTime.MinValue), "ISODate(\"0001-01-01T00:00:00Z\")"),
  266. new TestData<BsonDateTime>(new BsonDateTime(BsonConstants.UnixEpoch), "ISODate(\"1970-01-01T00:00:00Z\")"),
  267. new TestData<BsonDateTime>(new BsonDateTime(utcNowTruncated), isoDate),
  268. new TestData<BsonDateTime>(new BsonDateTime(DateTime.MaxValue), "ISODate(\"9999-12-31T23:59:59.999Z\")"),
  269. };
  270. foreach (var test in tests)
  271. {
  272. var json = test.Value.ToJson();
  273. Assert.AreEqual(test.Expected, json);
  274. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<BsonDateTime>(json));
  275. }
  276. }
  277. [Test]
  278. public void TestDateTimeStrict()
  279. {
  280. var utcNow = DateTime.UtcNow;
  281. var utcNowTruncated = utcNow.AddTicks(-(utcNow.Ticks % 10000));
  282. var ms = BsonUtils.ToMillisecondsSinceEpoch(utcNowTruncated);
  283. var strictDate = string.Format("{{ \"$date\" : {0} }}", ms);
  284. var tests = new TestData<BsonDateTime>[]
  285. {
  286. new TestData<BsonDateTime>(new BsonDateTime(long.MinValue), "{ \"$date\" : -9223372036854775808 }"),
  287. new TestData<BsonDateTime>(new BsonDateTime(0), "{ \"$date\" : 0 }"),
  288. new TestData<BsonDateTime>(new BsonDateTime(long.MaxValue), "{ \"$date\" : 9223372036854775807 }"),
  289. new TestData<BsonDateTime>(new BsonDateTime(DateTime.MinValue), "{ \"$date\" : -62135596800000 }"),
  290. new TestData<BsonDateTime>(new BsonDateTime(BsonConstants.UnixEpoch), "{ \"$date\" : 0 }"),
  291. new TestData<BsonDateTime>(new BsonDateTime(utcNowTruncated), strictDate),
  292. new TestData<BsonDateTime>(new BsonDateTime(DateTime.MaxValue), "{ \"$date\" : 253402300799999 }"),
  293. };
  294. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  295. foreach (var test in tests)
  296. {
  297. var json = test.Value.ToJson(jsonSettings);
  298. Assert.AreEqual(test.Expected, json);
  299. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<BsonDateTime>(json));
  300. }
  301. }
  302. [Test]
  303. public void TestJavaScript()
  304. {
  305. var document = new BsonDocument
  306. {
  307. { "f", new BsonJavaScript("function f() { return 1; }") }
  308. };
  309. string expected = "{ \"f\" : { \"$code\" : \"function f() { return 1; }\" } }";
  310. string actual = document.ToJson();
  311. Assert.AreEqual(expected, actual);
  312. }
  313. [Test]
  314. public void TestJavaScriptWithScope()
  315. {
  316. var document = new BsonDocument
  317. {
  318. { "f", new BsonJavaScriptWithScope("function f() { return n; }", new BsonDocument("n", 1)) }
  319. };
  320. string expected = "{ \"f\" : { \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } } }";
  321. string actual = document.ToJson();
  322. Assert.AreEqual(expected, actual);
  323. }
  324. [Test]
  325. public void TestGuid()
  326. {
  327. var document = new BsonDocument
  328. {
  329. { "guid", new Guid("B5F21E0C2A0D42d6AD03D827008D8AB6") }
  330. };
  331. string expected = "{ \"guid\" : CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\") }";
  332. string actual = document.ToJson();
  333. Assert.AreEqual(expected, actual);
  334. }
  335. [Test]
  336. public void TestMaxKey()
  337. {
  338. var document = new BsonDocument
  339. {
  340. { "maxkey", BsonMaxKey.Value }
  341. };
  342. string expected = "{ \"maxkey\" : MaxKey }";
  343. string actual = document.ToJson();
  344. Assert.AreEqual(expected, actual);
  345. }
  346. [Test]
  347. public void TestMinKey()
  348. {
  349. var document = new BsonDocument
  350. {
  351. { "minkey", BsonMinKey.Value }
  352. };
  353. string expected = "{ \"minkey\" : MinKey }";
  354. string actual = document.ToJson();
  355. Assert.AreEqual(expected, actual);
  356. }
  357. [Test]
  358. public void TestNull()
  359. {
  360. var document = new BsonDocument
  361. {
  362. { "null", BsonNull.Value }
  363. };
  364. string expected = "{ \"null\" : null }";
  365. string actual = document.ToJson();
  366. Assert.AreEqual(expected, actual);
  367. }
  368. [Test]
  369. public void TestObjectIdShell()
  370. {
  371. var objectId = new ObjectId("4d0ce088e447ad08b4721a37");
  372. var json = objectId.ToJson();
  373. var expected = "ObjectId(\"4d0ce088e447ad08b4721a37\")";
  374. Assert.AreEqual(expected, json);
  375. Assert.AreEqual(objectId, BsonSerializer.Deserialize<ObjectId>(json));
  376. }
  377. [Test]
  378. public void TestObjectIdStrict()
  379. {
  380. var objectId = new ObjectId("4d0ce088e447ad08b4721a37");
  381. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  382. var json = objectId.ToJson(jsonSettings);
  383. var expected = "{ \"$oid\" : \"4d0ce088e447ad08b4721a37\" }";
  384. Assert.AreEqual(expected, json);
  385. Assert.AreEqual(objectId, BsonSerializer.Deserialize<ObjectId>(json));
  386. }
  387. [Test]
  388. public void TestRegularExpressionShell()
  389. {
  390. var tests = new TestData<BsonRegularExpression>[]
  391. {
  392. new TestData<BsonRegularExpression>(new BsonRegularExpression(""), "/(?:)/"),
  393. new TestData<BsonRegularExpression>(new BsonRegularExpression("a"), "/a/"),
  394. new TestData<BsonRegularExpression>(new BsonRegularExpression("a/b"), "/a\\/b/"),
  395. new TestData<BsonRegularExpression>(new BsonRegularExpression("a\\b"), "/a\\b/"),
  396. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "i"), "/a/i"),
  397. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "m"), "/a/m"),
  398. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "x"), "/a/x"),
  399. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "s"), "/a/s"),
  400. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "imxs"), "/a/imxs"),
  401. };
  402. foreach (var test in tests)
  403. {
  404. var json = test.Value.ToJson();
  405. Assert.AreEqual(test.Expected, json);
  406. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<BsonRegularExpression>(json));
  407. }
  408. }
  409. [Test]
  410. public void TestRegularExpressionStrict()
  411. {
  412. var tests = new TestData<BsonRegularExpression>[]
  413. {
  414. new TestData<BsonRegularExpression>(new BsonRegularExpression(""), "{ \"$regex\" : \"\", \"$options\" : \"\" }"),
  415. new TestData<BsonRegularExpression>(new BsonRegularExpression("a"), "{ \"$regex\" : \"a\", \"$options\" : \"\" }"),
  416. new TestData<BsonRegularExpression>(new BsonRegularExpression("a/b"), "{ \"$regex\" : \"a/b\", \"$options\" : \"\" }"),
  417. new TestData<BsonRegularExpression>(new BsonRegularExpression("a\\b"), "{ \"$regex\" : \"a\\\\b\", \"$options\" : \"\" }"),
  418. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "i"), "{ \"$regex\" : \"a\", \"$options\" : \"i\" }"),
  419. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "m"), "{ \"$regex\" : \"a\", \"$options\" : \"m\" }"),
  420. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "x"), "{ \"$regex\" : \"a\", \"$options\" : \"x\" }"),
  421. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "s"), "{ \"$regex\" : \"a\", \"$options\" : \"s\" }"),
  422. new TestData<BsonRegularExpression>(new BsonRegularExpression("a", "imxs"), "{ \"$regex\" : \"a\", \"$options\" : \"imxs\" }"),
  423. };
  424. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  425. foreach (var test in tests)
  426. {
  427. var json = test.Value.ToJson(jsonSettings);
  428. Assert.AreEqual(test.Expected, json);
  429. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<BsonRegularExpression>(json));
  430. }
  431. }
  432. [Test]
  433. public void TestString()
  434. {
  435. var tests = new TestData<string>[]
  436. {
  437. new TestData<string>(null, "null"),
  438. new TestData<string>("", "\"\""),
  439. new TestData<string>(" ", "\" \""),
  440. new TestData<string>("a", "\"a\""),
  441. new TestData<string>("ab", "\"ab\""),
  442. new TestData<string>("abc", "\"abc\""),
  443. new TestData<string>("abc\0def", "\"abc\\u0000def\""),
  444. new TestData<string>("\'", "\"'\""),
  445. new TestData<string>("\"", "\"\\\"\""),
  446. new TestData<string>("\0", "\"\\u0000\""),
  447. new TestData<string>("\a", "\"\\u0007\""),
  448. new TestData<string>("\b", "\"\\b\""),
  449. new TestData<string>("\f", "\"\\f\""),
  450. new TestData<string>("\n", "\"\\n\""),
  451. new TestData<string>("\r", "\"\\r\""),
  452. new TestData<string>("\t", "\"\\t\""),
  453. new TestData<string>("\v", "\"\\u000b\""),
  454. new TestData<string>("\u0080", "\"\\u0080\""),
  455. new TestData<string>("\u0080\u0081", "\"\\u0080\\u0081\""),
  456. new TestData<string>("\u0080\u0081\u0082", "\"\\u0080\\u0081\\u0082\"")
  457. };
  458. foreach (var test in tests)
  459. {
  460. var json = test.Value.ToJson();
  461. Assert.AreEqual(test.Expected, json);
  462. Assert.AreEqual(test.Value, BsonSerializer.Deserialize<string>(json));
  463. }
  464. }
  465. [Test]
  466. public void TestSymbol()
  467. {
  468. var document = new BsonDocument
  469. {
  470. { "symbol", BsonSymbolTable.Lookup("name") }
  471. };
  472. string expected = "{ \"symbol\" : { \"$symbol\" : \"name\" } }";
  473. string actual = document.ToJson();
  474. Assert.AreEqual(expected, actual);
  475. }
  476. [Test]
  477. public void TestTimestamp()
  478. {
  479. var document = new BsonDocument
  480. {
  481. { "timestamp", new BsonTimestamp(1, 2) }
  482. };
  483. string expected = "{ \"timestamp\" : Timestamp(1, 2) }";
  484. string actual = document.ToJson();
  485. Assert.AreEqual(expected, actual);
  486. }
  487. [Test]
  488. public void TestUndefined()
  489. {
  490. var document = new BsonDocument
  491. {
  492. { "undefined", BsonUndefined.Value }
  493. };
  494. string expected = "{ \"undefined\" : undefined }";
  495. string actual = document.ToJson();
  496. Assert.AreEqual(expected, actual);
  497. }
  498. [Test]
  499. public void TestUtf16BigEndian()
  500. {
  501. var encoding = new UnicodeEncoding(true, true, true);
  502. using (var memoryStream = new MemoryStream())
  503. {
  504. using (var streamWriter = new StreamWriter(memoryStream, encoding))
  505. using (var jsonWriter = new JsonWriter(streamWriter, JsonWriterSettings.Defaults))
  506. {
  507. var document = new BsonDocument("x", 1);
  508. BsonSerializer.Serialize(jsonWriter, document);
  509. }
  510. var bytes = memoryStream.ToArray();
  511. var bom = new byte[] { 0xfe, 0xff };
  512. var expected = bom.Concat(encoding.GetBytes("{ \"x\" : 1 }")).ToArray();
  513. CollectionAssert.AreEqual(expected, bytes);
  514. }
  515. }
  516. [Test]
  517. public void TestUtf16LittleEndian()
  518. {
  519. var encoding = new UnicodeEncoding(false, true, true);
  520. using (var memoryStream = new MemoryStream())
  521. {
  522. using (var streamWriter = new StreamWriter(memoryStream, encoding))
  523. using (var jsonWriter = new JsonWriter(streamWriter, JsonWriterSettings.Defaults))
  524. {
  525. var document = new BsonDocument("x", 1);
  526. BsonSerializer.Serialize(jsonWriter, document);
  527. }
  528. var bytes = memoryStream.ToArray();
  529. var bom = new byte[] { 0xff, 0xfe };
  530. var expected = bom.Concat(encoding.GetBytes("{ \"x\" : 1 }")).ToArray();
  531. CollectionAssert.AreEqual(expected, bytes);
  532. }
  533. }
  534. [Test]
  535. public void TestUtf8()
  536. {
  537. var encoding = new UTF8Encoding(true, true); // emit UTF8 identifier
  538. using (var memoryStream = new MemoryStream())
  539. {
  540. using (var streamWriter = new StreamWriter(memoryStream, encoding))
  541. using (var jsonWriter = new JsonWriter(streamWriter, JsonWriterSettings.Defaults))
  542. {
  543. var document = new BsonDocument("x", 1);
  544. BsonSerializer.Serialize(jsonWriter, document);
  545. }
  546. var bytes = memoryStream.ToArray();
  547. var bom = new byte[] { 0xef, 0xbb, 0xbf };
  548. var expected = bom.Concat(encoding.GetBytes("{ \"x\" : 1 }")).ToArray();
  549. CollectionAssert.AreEqual(expected, bytes);
  550. }
  551. }
  552. }
  553. }