PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionarySerializerTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 631 lines | 557 code | 58 blank | 16 comment | 41 complexity | 7ed4cf56bd371802916c01dbe6f4c747 MD5 | raw file
Possible License(s): Apache-2.0
  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;
  17. using System.Collections.Specialized;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Text;
  21. using FluentAssertions;
  22. using MongoDB.Bson.IO;
  23. using MongoDB.Bson.Serialization;
  24. using MongoDB.Bson.Serialization.Attributes;
  25. using MongoDB.Bson.TestHelpers;
  26. using MongoDB.Bson.TestHelpers.XunitExtensions;
  27. using Xunit;
  28. namespace MongoDB.Bson.Tests.Serialization.DictionarySerializers
  29. {
  30. [BsonDiscriminator("DictionarySerializers.C")] // "C" is an ambiguous discriminator when nominalType is System.Object
  31. public class C
  32. {
  33. public string P { get; set; }
  34. public override bool Equals(object obj)
  35. {
  36. if (obj == null || obj.GetType() != typeof(C))
  37. {
  38. return false;
  39. }
  40. var other = (C)obj;
  41. return P.Equals(other.P);
  42. }
  43. public override int GetHashCode()
  44. {
  45. return 0;
  46. }
  47. }
  48. public class DictionarySerializerTests
  49. {
  50. public class T
  51. {
  52. public Hashtable HT { get; set; }
  53. public IDictionary ID { get; set; }
  54. public ListDictionary LD { get; set; }
  55. public OrderedDictionary OD { get; set; }
  56. public SortedList SL { get; set; }
  57. }
  58. [Fact]
  59. public void TestNull()
  60. {
  61. var obj = new T { HT = null, ID = null, LD = null, OD = null, SL = null };
  62. var json = obj.ToJson();
  63. var rep = "null";
  64. var expected = "{ 'HT' : #R, 'ID' : #R, 'LD' : #R, 'OD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
  65. Assert.Equal(expected, json);
  66. var bson = obj.ToBson();
  67. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  68. Assert.Null(rehydrated.HT);
  69. Assert.Null(rehydrated.ID);
  70. Assert.Null(rehydrated.LD);
  71. Assert.Null(rehydrated.OD);
  72. Assert.Null(rehydrated.SL);
  73. Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
  74. }
  75. [Fact]
  76. public void TestEmpty()
  77. {
  78. var ht = new Hashtable();
  79. var ld = CreateListDictionary(ht);
  80. var od = CreateOrderedDictionary(ht);
  81. var sl = CreateSortedList(ht);
  82. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  83. var json = obj.ToJson();
  84. var rep = "{ }";
  85. var expected = "{ 'HT' : #R, 'ID' : #R, 'LD' : #R, 'OD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
  86. Assert.Equal(expected, json);
  87. var bson = obj.ToBson();
  88. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  89. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  90. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  91. rehydrated.LD.Should().Equal(obj.LD);
  92. rehydrated.OD.Should().Equal(obj.OD);
  93. rehydrated.LD.Should().Equal(obj.LD);
  94. }
  95. [Fact]
  96. public void TestOneC()
  97. {
  98. var ht = new Hashtable { { "A", new C { P = "x" } } };
  99. var ld = CreateListDictionary(ht);
  100. var od = CreateOrderedDictionary(ht);
  101. var sl = CreateSortedList(ht);
  102. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  103. var json = obj.ToJson();
  104. var rep = "{ 'A' : { '_t' : 'DictionarySerializers.C', 'P' : 'x' } }";
  105. var expected = "{ 'HT' : #R, 'ID' : #R, 'LD' : #R, 'OD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
  106. Assert.Equal(expected, json);
  107. var bson = obj.ToBson();
  108. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  109. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  110. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  111. rehydrated.LD.Should().Equal(obj.LD);
  112. rehydrated.OD.Should().Equal(obj.OD);
  113. rehydrated.LD.Should().Equal(obj.LD);
  114. }
  115. [Fact]
  116. public void TestOneInt()
  117. {
  118. var ht = new Hashtable { { "A", 1 } };
  119. var ld = CreateListDictionary(ht);
  120. var od = CreateOrderedDictionary(ht);
  121. var sl = CreateSortedList(ht);
  122. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  123. var json = obj.ToJson();
  124. var rep = "{ 'A' : 1 }";
  125. var expected = "{ 'HT' : #R, 'ID' : #R, 'LD' : #R, 'OD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
  126. Assert.Equal(expected, json);
  127. var bson = obj.ToBson();
  128. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  129. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  130. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  131. rehydrated.LD.Should().Equal(obj.LD);
  132. rehydrated.OD.Should().Equal(obj.OD);
  133. rehydrated.LD.Should().Equal(obj.LD);
  134. }
  135. [Fact]
  136. public void TestOneIntWithIntKey()
  137. {
  138. var ht = new Hashtable { { 1, 2 } };
  139. var ld = CreateListDictionary(ht);
  140. var od = CreateOrderedDictionary(ht);
  141. var sl = CreateSortedList(ht);
  142. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  143. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  144. }
  145. [Fact]
  146. public void TestOneString()
  147. {
  148. var ht = new Hashtable { { "A", "x" } };
  149. var ld = CreateListDictionary(ht);
  150. var od = CreateOrderedDictionary(ht);
  151. var sl = CreateSortedList(ht);
  152. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  153. var json = obj.ToJson();
  154. var rep = "{ 'A' : 'x' }";
  155. var expected = "{ 'HT' : #R, 'ID' : #R, 'LD' : #R, 'OD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\"");
  156. Assert.Equal(expected, json);
  157. var bson = obj.ToBson();
  158. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  159. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  160. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  161. rehydrated.LD.Should().Equal(obj.LD);
  162. rehydrated.OD.Should().Equal(obj.OD);
  163. rehydrated.LD.Should().Equal(obj.LD);
  164. }
  165. [Fact]
  166. public void TestOneStringWithIntKey()
  167. {
  168. var ht = new Hashtable { { 1, "x" } };
  169. var ld = CreateListDictionary(ht);
  170. var od = CreateOrderedDictionary(ht);
  171. var sl = CreateSortedList(ht);
  172. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  173. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  174. }
  175. [Fact]
  176. public void TestTwoCs()
  177. {
  178. var ht = new Hashtable { { "A", new C { P = "x" } }, { "B", new C { P = "y" } } };
  179. var ld = CreateListDictionary(ht);
  180. var od = CreateOrderedDictionary(ht);
  181. var sl = CreateSortedList(ht);
  182. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  183. var json = obj.ToJson();
  184. var reps = new Hashtable
  185. {
  186. { "A", "{ '_t' : 'DictionarySerializers.C', 'P' : 'x' }"},
  187. { "B", "{ '_t' : 'DictionarySerializers.C', 'P' : 'y' }"}
  188. };
  189. var htRep = GetDocumentRepresentationInKeyOrder(ht, reps);
  190. var ldRep = GetDocumentRepresentationInKeyOrder(ld, reps);
  191. var odRep = GetDocumentRepresentationInKeyOrder(od, reps);
  192. var slRep = GetDocumentRepresentationInKeyOrder(sl, reps);
  193. var expected = "{ 'HT' : #HT, 'ID' : #HT, 'LD' : #LD, 'OD' : #OD, 'SL' : #SL }";
  194. expected = expected.Replace("#HT", htRep);
  195. expected = expected.Replace("#LD", ldRep);
  196. expected = expected.Replace("#OD", odRep);
  197. expected = expected.Replace("#SL", slRep);
  198. expected = expected.Replace("'", "\"");
  199. Assert.Equal(expected, json);
  200. var bson = obj.ToBson();
  201. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  202. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  203. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  204. rehydrated.LD.Should().Equal(obj.LD);
  205. rehydrated.OD.Should().Equal(obj.OD);
  206. rehydrated.LD.Should().Equal(obj.LD);
  207. }
  208. [Fact]
  209. public void TestTwoCsWithIntKeys()
  210. {
  211. var ht = new Hashtable { { 1, new C { P = "x" } }, { 2, new C { P = "y" } } };
  212. var ld = CreateListDictionary(ht);
  213. var od = CreateOrderedDictionary(ht);
  214. var sl = CreateSortedList(ht);
  215. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  216. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  217. }
  218. [Fact]
  219. public void TestTwoInts()
  220. {
  221. var ht = new Hashtable { { "A", 1 }, { "B", 2 } };
  222. var ld = CreateListDictionary(ht);
  223. var od = CreateOrderedDictionary(ht);
  224. var sl = CreateSortedList(ht);
  225. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  226. var json = obj.ToJson();
  227. var reps = new Hashtable
  228. {
  229. { "A", "1"},
  230. { "B", "2"}
  231. };
  232. var htRep = GetDocumentRepresentationInKeyOrder(ht, reps);
  233. var ldRep = GetDocumentRepresentationInKeyOrder(ld, reps);
  234. var odRep = GetDocumentRepresentationInKeyOrder(od, reps);
  235. var slRep = GetDocumentRepresentationInKeyOrder(sl, reps);
  236. var expected = "{ 'HT' : #HT, 'ID' : #HT, 'LD' : #LD, 'OD' : #OD, 'SL' : #SL }";
  237. expected = expected.Replace("#HT", htRep);
  238. expected = expected.Replace("#LD", ldRep);
  239. expected = expected.Replace("#OD", odRep);
  240. expected = expected.Replace("#SL", slRep);
  241. expected = expected.Replace("'", "\"");
  242. Assert.Equal(expected, json);
  243. var bson = obj.ToBson();
  244. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  245. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  246. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  247. rehydrated.LD.Should().Equal(obj.LD);
  248. rehydrated.OD.Should().Equal(obj.OD);
  249. rehydrated.LD.Should().Equal(obj.LD);
  250. }
  251. [Fact]
  252. public void TestTwoIntsWithIntKeys()
  253. {
  254. var ht = new Hashtable { { 1, 2 }, { 3, 4 } };
  255. var ld = CreateListDictionary(ht);
  256. var od = CreateOrderedDictionary(ht);
  257. var sl = CreateSortedList(ht);
  258. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  259. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  260. }
  261. [Fact]
  262. public void TestTwoStrings()
  263. {
  264. var ht = new Hashtable { { "A", "x" }, { "B", "y" } };
  265. var ld = CreateListDictionary(ht);
  266. var od = CreateOrderedDictionary(ht);
  267. var sl = CreateSortedList(ht);
  268. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  269. var json = obj.ToJson();
  270. var reps = new Hashtable
  271. {
  272. { "A", "'x'"},
  273. { "B", "'y'"}
  274. };
  275. var htRep = GetDocumentRepresentationInKeyOrder(ht, reps);
  276. var ldRep = GetDocumentRepresentationInKeyOrder(ld, reps);
  277. var odRep = GetDocumentRepresentationInKeyOrder(od, reps);
  278. var slRep = GetDocumentRepresentationInKeyOrder(sl, reps);
  279. var expected = "{ 'HT' : #HT, 'ID' : #HT, 'LD' : #LD, 'OD' : #OD, 'SL' : #SL }";
  280. expected = expected.Replace("#HT", htRep);
  281. expected = expected.Replace("#LD", ldRep);
  282. expected = expected.Replace("#OD", odRep);
  283. expected = expected.Replace("#SL", slRep);
  284. expected = expected.Replace("'", "\"");
  285. Assert.Equal(expected, json);
  286. var bson = obj.ToBson();
  287. var rehydrated = BsonSerializer.Deserialize<T>(bson);
  288. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  289. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  290. rehydrated.LD.Should().Equal(obj.LD);
  291. rehydrated.OD.Should().Equal(obj.OD);
  292. rehydrated.LD.Should().Equal(obj.LD);
  293. }
  294. [Fact]
  295. public void TestTwoStringsWithIntKeys()
  296. {
  297. var ht = new Hashtable { { 1, "x" }, { 2, "y" } };
  298. var ld = CreateListDictionary(ht);
  299. var od = CreateOrderedDictionary(ht);
  300. var sl = CreateSortedList(ht);
  301. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  302. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  303. }
  304. [Theory]
  305. [ParameterAttributeData]
  306. [ResetGuidModeAfterTest]
  307. public void TestMixedPrimitiveTypes(
  308. [ClassValues(typeof(GuidModeValues))] GuidMode mode)
  309. {
  310. mode.Set();
  311. #pragma warning disable 618
  312. var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
  313. var isoDate = dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
  314. var guid = Guid.Empty;
  315. string expectedGuidJson = null;
  316. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  317. {
  318. switch (BsonDefaults.GuidRepresentation)
  319. {
  320. case GuidRepresentation.CSharpLegacy: expectedGuidJson = "CSUUID('00000000-0000-0000-0000-000000000000')"; break;
  321. case GuidRepresentation.JavaLegacy: expectedGuidJson = "JUUID('00000000-0000-0000-0000-000000000000')"; break;
  322. case GuidRepresentation.PythonLegacy: expectedGuidJson = "PYUUID('00000000-0000-0000-0000-000000000000')"; break;
  323. case GuidRepresentation.Standard: expectedGuidJson = "UUID('00000000-0000-0000-0000-000000000000')"; break;
  324. }
  325. }
  326. var objectId = ObjectId.Empty;
  327. var ht = new Hashtable
  328. {
  329. { "A", true },
  330. { "B", dateTime },
  331. { "C", 1.5 },
  332. { "D", 1 },
  333. { "E", 2L },
  334. { "G", objectId },
  335. { "H", "x" }
  336. };
  337. if (expectedGuidJson != null)
  338. {
  339. ht.Add("F", guid);
  340. }
  341. var ld = CreateListDictionary(ht);
  342. var od = CreateOrderedDictionary(ht);
  343. var sl = CreateSortedList(ht);
  344. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  345. var json = obj.ToJson(new JsonWriterSettings());
  346. var reps = new Hashtable
  347. {
  348. { "A", "true" },
  349. { "B", "ISODate('#')".Replace("#", isoDate) },
  350. { "C", "1.5" },
  351. { "D", "1" },
  352. { "E", "NumberLong(2)" },
  353. { "G", "ObjectId('000000000000000000000000')" },
  354. { "H", "'x'" }
  355. };
  356. if (expectedGuidJson != null)
  357. {
  358. reps.Add("F", expectedGuidJson);
  359. }
  360. var htRep = GetDocumentRepresentationInKeyOrder(ht, reps);
  361. var ldRep = GetDocumentRepresentationInKeyOrder(ld, reps);
  362. var odRep = GetDocumentRepresentationInKeyOrder(od, reps);
  363. var slRep = GetDocumentRepresentationInKeyOrder(sl, reps);
  364. var expected = "{ 'HT' : #HT, 'ID' : #HT, 'LD' : #LD, 'OD' : #OD, 'SL' : #SL }";
  365. expected = expected.Replace("#HT", htRep);
  366. expected = expected.Replace("#LD", ldRep);
  367. expected = expected.Replace("#OD", odRep);
  368. expected = expected.Replace("#SL", slRep);
  369. expected = expected.Replace("'", "\"");
  370. Assert.Equal(expected, json);
  371. var bson = obj.ToBson(writerSettings: new BsonBinaryWriterSettings());
  372. var rehydrated = BsonSerializer.Deserialize<T>(new BsonBinaryReader(new MemoryStream(bson), new BsonBinaryReaderSettings()));
  373. rehydrated.HT.Should().BeEquivalentTo(obj.HT);
  374. rehydrated.ID.Should().BeEquivalentTo(obj.ID);
  375. rehydrated.LD.Should().Equal(obj.LD);
  376. rehydrated.OD.Should().Equal(obj.OD);
  377. rehydrated.LD.Should().Equal(obj.LD);
  378. #pragma warning restore 618
  379. }
  380. [Fact]
  381. public void TestMixedPrimitiveTypesWithIntKeys()
  382. {
  383. var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
  384. var guid = Guid.Empty;
  385. var objectId = ObjectId.Empty;
  386. var ht = new Hashtable
  387. {
  388. { 1, true },
  389. { 2, dateTime },
  390. { 3, 1.5 },
  391. { 4, 1 },
  392. { 5, 2L },
  393. { 6, guid },
  394. { 7, objectId },
  395. { 8, "x" }
  396. };
  397. var ld = CreateListDictionary(ht);
  398. var od = CreateOrderedDictionary(ht);
  399. var sl = CreateSortedList(ht);
  400. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od, SL = sl };
  401. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  402. }
  403. [Fact]
  404. public void TestMixedPrimitiveTypesWithMixedKeys()
  405. {
  406. // note: no SortedList in this test because you can't sort a set of keys that have mixed types
  407. var dateTime = DateTime.SpecifyKind(new DateTime(2010, 1, 1, 11, 22, 33), DateTimeKind.Utc);
  408. var guid = Guid.Empty;
  409. var objectId = ObjectId.Empty;
  410. var ht = new Hashtable
  411. {
  412. { "A", true },
  413. { "B", dateTime },
  414. { "C", 1.5 },
  415. { "D", 1 },
  416. { 4, 2L },
  417. { 5.0, guid },
  418. { true, objectId },
  419. { false, "x" }
  420. };
  421. var ld = CreateListDictionary(ht);
  422. var od = CreateOrderedDictionary(ht);
  423. var obj = new T { HT = ht, ID = ht, LD = ld, OD = od };
  424. Assert.Throws<BsonSerializationException>(() => obj.ToBson());
  425. }
  426. public bool CompareDictionaries(IDictionary dictionary1, IDictionary dictionary2)
  427. {
  428. if (object.ReferenceEquals(dictionary1, dictionary2)) { return true; }
  429. if (dictionary1 == null) { return false; }
  430. if (dictionary2 == null) { return false; }
  431. if (dictionary1.Count != dictionary2.Count) { return false; }
  432. foreach (var key in dictionary1.Keys)
  433. {
  434. var item1 = dictionary1[key];
  435. var item2 = dictionary2[key];
  436. if (object.ReferenceEquals(item1, item2)) { continue; }
  437. if (item1 == null) { return false; }
  438. if (item2 == null) { return false; }
  439. if (!item1.Equals(item2)) { return false; }
  440. }
  441. return true;
  442. }
  443. public bool CompareOrderedDictionaries(IOrderedDictionary dictionary1, IOrderedDictionary dictionary2)
  444. {
  445. if (object.ReferenceEquals(dictionary1, dictionary2)) { return true; }
  446. if (dictionary1 == null) { return false; }
  447. if (dictionary2 == null) { return false; }
  448. if (dictionary1.Count != dictionary2.Count) { return false; }
  449. if (!dictionary1.Keys.Cast<object>().SequenceEqual(dictionary2.Keys.Cast<Object>())) { return false; }
  450. for (int i = 0; i < dictionary1.Count; i++)
  451. {
  452. var item1 = dictionary1[i];
  453. var item2 = dictionary2[i];
  454. if (object.ReferenceEquals(item1, item2)) { continue; }
  455. if (item1 == null) { return false; }
  456. if (item2 == null) { return false; }
  457. if (!item1.Equals(item2)) { return false; }
  458. }
  459. return true;
  460. }
  461. private ListDictionary CreateListDictionary(Hashtable ht)
  462. {
  463. var ld = new ListDictionary();
  464. foreach (DictionaryEntry entry in ht)
  465. {
  466. ld.Add(entry.Key, entry.Value);
  467. }
  468. return ld;
  469. }
  470. private OrderedDictionary CreateOrderedDictionary(Hashtable ht)
  471. {
  472. var od = new OrderedDictionary();
  473. foreach (DictionaryEntry entry in ht)
  474. {
  475. od.Add(entry.Key, entry.Value);
  476. }
  477. return od;
  478. }
  479. private SortedList CreateSortedList(Hashtable ht)
  480. {
  481. var sl = new SortedList();
  482. foreach (DictionaryEntry entry in ht)
  483. {
  484. sl.Add(entry.Key, entry.Value);
  485. }
  486. return sl;
  487. }
  488. private string GetArrayRepresentationInKeyOrder(IDictionary dictionary, IDictionary representations)
  489. {
  490. var sb = new StringBuilder();
  491. foreach (var key in dictionary.Keys)
  492. {
  493. sb.Append((sb.Length == 0) ? "[" : ", ");
  494. sb.Append(representations[key]);
  495. }
  496. sb.Append("]");
  497. return sb.ToString();
  498. }
  499. private string GetDocumentRepresentationInKeyOrder(IDictionary dictionary, IDictionary representations)
  500. {
  501. var sb = new StringBuilder();
  502. foreach (var key in dictionary.Keys)
  503. {
  504. sb.Append((sb.Length == 0) ? "{ " : ", ");
  505. sb.AppendFormat("'{0}' : {1}", key, representations[key]);
  506. }
  507. sb.Append(" }");
  508. return sb.ToString();
  509. }
  510. }
  511. public class EnumDictionaryTests
  512. {
  513. private enum E
  514. {
  515. None,
  516. A,
  517. B
  518. }
  519. private class C
  520. {
  521. public Hashtable Hashtable;
  522. }
  523. // required for deterministic tests
  524. private class D
  525. {
  526. public SortedList Hashtable;
  527. }
  528. [Fact]
  529. public void TestSerializeNull()
  530. {
  531. C c = new C { Hashtable = null };
  532. var json = c.ToJson();
  533. var expected = ("{ 'Hashtable' : null }").Replace("'", "\"");
  534. Assert.Equal(expected, json);
  535. var bson = c.ToBson();
  536. var rehydrated = BsonSerializer.Deserialize<C>(bson);
  537. Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
  538. }
  539. [Fact]
  540. public void TestSerializeEmpty()
  541. {
  542. C c = new C { Hashtable = new Hashtable() };
  543. var json = c.ToJson();
  544. var expected = ("{ 'Hashtable' : { } }").Replace("'", "\"");
  545. Assert.Equal(expected, json);
  546. var bson = c.ToBson();
  547. var rehydrated = BsonSerializer.Deserialize<C>(bson);
  548. Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
  549. }
  550. [Fact]
  551. public void TestSerialize1()
  552. {
  553. C c = new C { Hashtable = new Hashtable { { "a", E.A } } };
  554. var json = c.ToJson();
  555. var expected = ("{ 'Hashtable' : { \"a\" : 1 } }").Replace("'", "\"");
  556. Assert.Equal(expected, json);
  557. var bson = c.ToBson();
  558. var rehydrated = BsonSerializer.Deserialize<C>(bson);
  559. Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
  560. }
  561. [Fact]
  562. public void TestSerialize2()
  563. {
  564. D d = new D { Hashtable = new SortedList { { "a", E.A }, { "b", E.B } } };
  565. var json = d.ToJson();
  566. var expected = ("{ 'Hashtable' : { \"a\" : 1, \"b\" : 2 } }").Replace("'", "\"");
  567. Assert.Equal(expected, json);
  568. var bson = d.ToBson();
  569. var rehydrated = BsonSerializer.Deserialize<D>(bson);
  570. Assert.True(bson.SequenceEqual(rehydrated.ToBson()));
  571. }
  572. }
  573. }