BsonClassMapSerializer.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /* Copyright 2010-2016 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.ComponentModel;
  18. using System.Linq;
  19. using System.Reflection;
  20. using MongoDB.Bson.IO;
  21. using MongoDB.Bson.Serialization.Serializers;
  22. namespace MongoDB.Bson.Serialization
  23. {
  24. /// <summary>
  25. /// Represents a serializer for a class map.
  26. /// </summary>
  27. /// <typeparam name="TClass">The type of the class.</typeparam>
  28. public class BsonClassMapSerializer<TClass> : SerializerBase<TClass>, IBsonIdProvider, IBsonDocumentSerializer, IBsonPolymorphicSerializer
  29. {
  30. // private fields
  31. private BsonClassMap _classMap;
  32. // constructors
  33. /// <summary>
  34. /// Initializes a new instance of the BsonClassMapSerializer class.
  35. /// </summary>
  36. /// <param name="classMap">The class map.</param>
  37. public BsonClassMapSerializer(BsonClassMap classMap)
  38. {
  39. if (classMap == null)
  40. {
  41. throw new ArgumentNullException("classMap");
  42. }
  43. if (classMap.ClassType != typeof(TClass))
  44. {
  45. var message = string.Format("Must be a BsonClassMap for the type {0}.", typeof(TClass));
  46. throw new ArgumentException(message, "classMap");
  47. }
  48. if (!classMap.IsFrozen)
  49. {
  50. throw new ArgumentException("Class map is not frozen.", nameof(classMap));
  51. }
  52. _classMap = classMap;
  53. }
  54. // public properties
  55. /// <summary>
  56. /// Gets a value indicating whether this serializer's discriminator is compatible with the object serializer.
  57. /// </summary>
  58. /// <value>
  59. /// <c>true</c> if this serializer's discriminator is compatible with the object serializer; otherwise, <c>false</c>.
  60. /// </value>
  61. public bool IsDiscriminatorCompatibleWithObjectSerializer
  62. {
  63. get { return true; }
  64. }
  65. // public methods
  66. /// <summary>
  67. /// Deserializes a value.
  68. /// </summary>
  69. /// <param name="context">The deserialization context.</param>
  70. /// <param name="args">The deserialization args.</param>
  71. /// <returns>A deserialized value.</returns>
  72. public override TClass Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  73. {
  74. var bsonReader = context.Reader;
  75. if (_classMap.ClassType.GetTypeInfo().IsValueType)
  76. {
  77. var message = string.Format("Value class {0} cannot be deserialized.", _classMap.ClassType.FullName);
  78. throw new BsonSerializationException(message);
  79. }
  80. if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
  81. {
  82. bsonReader.ReadNull();
  83. return default(TClass);
  84. }
  85. else
  86. {
  87. var discriminatorConvention = _classMap.GetDiscriminatorConvention();
  88. var actualType = discriminatorConvention.GetActualType(bsonReader, args.NominalType);
  89. if (actualType == typeof(TClass))
  90. {
  91. return DeserializeClass(context);
  92. }
  93. else
  94. {
  95. var serializer = BsonSerializer.LookupSerializer(actualType);
  96. return (TClass)serializer.Deserialize(context);
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Deserializes a value.
  102. /// </summary>
  103. /// <param name="context">The deserialization context.</param>
  104. /// <returns>A deserialized value.</returns>
  105. public TClass DeserializeClass(BsonDeserializationContext context)
  106. {
  107. var bsonReader = context.Reader;
  108. var bsonType = bsonReader.GetCurrentBsonType();
  109. if (bsonType != BsonType.Document)
  110. {
  111. var message = string.Format(
  112. "Expected a nested document representing the serialized form of a {0} value, but found a value of type {1} instead.",
  113. typeof(TClass).FullName, bsonType);
  114. throw new FormatException(message);
  115. }
  116. Dictionary<string, object> values = null;
  117. var document = default(TClass);
  118. #if NET45 || NETSTANDARD2_0
  119. ISupportInitialize supportsInitialization = null;
  120. #endif
  121. if (_classMap.HasCreatorMaps)
  122. {
  123. // for creator-based deserialization we first gather the values in a dictionary and then call a matching creator
  124. values = new Dictionary<string, object>();
  125. }
  126. else
  127. {
  128. // for mutable classes we deserialize the values directly into the result object
  129. document = (TClass)_classMap.CreateInstance();
  130. #if NET45 || NETSTANDARD2_0
  131. supportsInitialization = document as ISupportInitialize;
  132. if (supportsInitialization != null)
  133. {
  134. supportsInitialization.BeginInit();
  135. }
  136. #endif
  137. }
  138. var discriminatorConvention = _classMap.GetDiscriminatorConvention();
  139. var allMemberMaps = _classMap.AllMemberMaps;
  140. var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex;
  141. var memberMapBitArray = FastMemberMapHelper.GetBitArray(allMemberMaps.Count);
  142. bsonReader.ReadStartDocument();
  143. var elementTrie = _classMap.ElementTrie;
  144. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  145. {
  146. var trieDecoder = new TrieNameDecoder<int>(elementTrie);
  147. var elementName = bsonReader.ReadName(trieDecoder);
  148. if (trieDecoder.Found)
  149. {
  150. var memberMapIndex = trieDecoder.Value;
  151. var memberMap = allMemberMaps[memberMapIndex];
  152. if (memberMapIndex != extraElementsMemberMapIndex)
  153. {
  154. if (document != null)
  155. {
  156. if (memberMap.IsReadOnly)
  157. {
  158. bsonReader.SkipValue();
  159. }
  160. else
  161. {
  162. var value = DeserializeMemberValue(context, memberMap);
  163. memberMap.Setter(document, value);
  164. }
  165. }
  166. else
  167. {
  168. var value = DeserializeMemberValue(context, memberMap);
  169. values[elementName] = value;
  170. }
  171. }
  172. else
  173. {
  174. if (document != null)
  175. {
  176. DeserializeExtraElementMember(context, document, elementName, memberMap);
  177. }
  178. else
  179. {
  180. DeserializeExtraElementValue(context, values, elementName, memberMap);
  181. }
  182. }
  183. memberMapBitArray[memberMapIndex >> 5] |= 1U << (memberMapIndex & 31);
  184. }
  185. else
  186. {
  187. if (elementName == discriminatorConvention.ElementName)
  188. {
  189. bsonReader.SkipValue(); // skip over discriminator
  190. continue;
  191. }
  192. if (extraElementsMemberMapIndex >= 0)
  193. {
  194. var extraElementsMemberMap = _classMap.ExtraElementsMemberMap;
  195. if (document != null)
  196. {
  197. DeserializeExtraElementMember(context, document, elementName, extraElementsMemberMap);
  198. }
  199. else
  200. {
  201. DeserializeExtraElementValue(context, values, elementName, extraElementsMemberMap);
  202. }
  203. memberMapBitArray[extraElementsMemberMapIndex >> 5] |= 1U << (extraElementsMemberMapIndex & 31);
  204. }
  205. else if (_classMap.IgnoreExtraElements)
  206. {
  207. bsonReader.SkipValue();
  208. }
  209. else
  210. {
  211. var message = string.Format(
  212. "Element '{0}' does not match any field or property of class {1}.",
  213. elementName, _classMap.ClassType.FullName);
  214. throw new FormatException(message);
  215. }
  216. }
  217. }
  218. bsonReader.ReadEndDocument();
  219. // check any members left over that we didn't have elements for (in blocks of 32 elements at a time)
  220. for (var bitArrayIndex = 0; bitArrayIndex < memberMapBitArray.Length; ++bitArrayIndex)
  221. {
  222. var memberMapIndex = bitArrayIndex << 5;
  223. var memberMapBlock = ~memberMapBitArray[bitArrayIndex]; // notice that bits are flipped so 1's are now the missing elements
  224. // work through this memberMapBlock of 32 elements
  225. while (true)
  226. {
  227. // examine missing elements (memberMapBlock is shifted right as we work through the block)
  228. for (; (memberMapBlock & 1) != 0; ++memberMapIndex, memberMapBlock >>= 1)
  229. {
  230. var memberMap = allMemberMaps[memberMapIndex];
  231. if (memberMap.IsReadOnly)
  232. {
  233. continue;
  234. }
  235. if (memberMap.IsRequired)
  236. {
  237. var fieldOrProperty = (memberMap.MemberInfo is FieldInfo) ? "field" : "property";
  238. var message = string.Format(
  239. "Required element '{0}' for {1} '{2}' of class {3} is missing.",
  240. memberMap.ElementName, fieldOrProperty, memberMap.MemberName, _classMap.ClassType.FullName);
  241. throw new FormatException(message);
  242. }
  243. if (document != null)
  244. {
  245. memberMap.ApplyDefaultValue(document);
  246. }
  247. else if (memberMap.IsDefaultValueSpecified && !memberMap.IsReadOnly)
  248. {
  249. values[memberMap.ElementName] = memberMap.DefaultValue;
  250. }
  251. }
  252. if (memberMapBlock == 0)
  253. {
  254. break;
  255. }
  256. // skip ahead to the next missing element
  257. var leastSignificantBit = FastMemberMapHelper.GetLeastSignificantBit(memberMapBlock);
  258. memberMapIndex += leastSignificantBit;
  259. memberMapBlock >>= leastSignificantBit;
  260. }
  261. }
  262. if (document != null)
  263. {
  264. #if NET45 || NETSTANDARD2_0
  265. if (supportsInitialization != null)
  266. {
  267. supportsInitialization.EndInit();
  268. }
  269. #endif
  270. return document;
  271. }
  272. else
  273. {
  274. return CreateInstanceUsingCreator(values);
  275. }
  276. }
  277. /// <summary>
  278. /// Gets the document Id.
  279. /// </summary>
  280. /// <param name="document">The document.</param>
  281. /// <param name="id">The Id.</param>
  282. /// <param name="idNominalType">The nominal type of the Id.</param>
  283. /// <param name="idGenerator">The IdGenerator for the Id type.</param>
  284. /// <returns>True if the document has an Id.</returns>
  285. public bool GetDocumentId(
  286. object document,
  287. out object id,
  288. out Type idNominalType,
  289. out IIdGenerator idGenerator)
  290. {
  291. var idMemberMap = _classMap.IdMemberMap;
  292. if (idMemberMap != null)
  293. {
  294. id = idMemberMap.Getter(document);
  295. idNominalType = idMemberMap.MemberType;
  296. idGenerator = idMemberMap.IdGenerator;
  297. return true;
  298. }
  299. else
  300. {
  301. id = null;
  302. idNominalType = null;
  303. idGenerator = null;
  304. return false;
  305. }
  306. }
  307. /// <summary>
  308. /// Tries to get the serialization info for a member.
  309. /// </summary>
  310. /// <param name="memberName">Name of the member.</param>
  311. /// <param name="serializationInfo">The serialization information.</param>
  312. /// <returns>
  313. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  314. /// </returns>
  315. public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
  316. {
  317. foreach (var memberMap in _classMap.AllMemberMaps)
  318. {
  319. if (memberMap.MemberName == memberName)
  320. {
  321. var elementName = memberMap.ElementName;
  322. var serializer = memberMap.GetSerializer();
  323. serializationInfo = new BsonSerializationInfo(elementName, serializer, serializer.ValueType);
  324. return true;
  325. }
  326. }
  327. serializationInfo = null;
  328. return false;
  329. }
  330. /// <summary>
  331. /// Serializes a value.
  332. /// </summary>
  333. /// <param name="context">The serialization context.</param>
  334. /// <param name="args">The serialization args.</param>
  335. /// <param name="value">The object.</param>
  336. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
  337. {
  338. var bsonWriter = context.Writer;
  339. if (value == null)
  340. {
  341. bsonWriter.WriteNull();
  342. }
  343. else
  344. {
  345. var actualType = value.GetType();
  346. if (actualType == typeof(TClass))
  347. {
  348. SerializeClass(context, args, value);
  349. }
  350. else
  351. {
  352. var serializer = BsonSerializer.LookupSerializer(actualType);
  353. serializer.Serialize(context, args, value);
  354. }
  355. }
  356. }
  357. /// <summary>
  358. /// Sets the document Id.
  359. /// </summary>
  360. /// <param name="document">The document.</param>
  361. /// <param name="id">The Id.</param>
  362. public void SetDocumentId(object document, object id)
  363. {
  364. var documentType = document.GetType();
  365. var documentTypeInfo = documentType.GetTypeInfo();
  366. if (documentTypeInfo.IsValueType)
  367. {
  368. var message = string.Format("SetDocumentId cannot be used with value type {0}.", documentType.FullName);
  369. throw new BsonSerializationException(message);
  370. }
  371. var idMemberMap = _classMap.IdMemberMap;
  372. if (idMemberMap != null)
  373. {
  374. idMemberMap.Setter(document, id);
  375. }
  376. else
  377. {
  378. var message = string.Format("Class {0} has no Id member.", document.GetType().FullName);
  379. throw new InvalidOperationException(message);
  380. }
  381. }
  382. // private methods
  383. private BsonCreatorMap ChooseBestCreator(Dictionary<string, object> values)
  384. {
  385. // there's only one selector for now, but there might be more in the future (possibly even user provided)
  386. var selector = new MostArgumentsCreatorSelector();
  387. var creatorMap = selector.SelectCreator(_classMap, values);
  388. if (creatorMap == null)
  389. {
  390. throw new BsonSerializationException("No matching creator found.");
  391. }
  392. return creatorMap;
  393. }
  394. private TClass CreateInstanceUsingCreator(Dictionary<string, object> values)
  395. {
  396. var creatorMap = ChooseBestCreator(values);
  397. var document = creatorMap.CreateInstance(values); // removes values consumed
  398. #if NET45 || NETSTANDARD2_0
  399. var supportsInitialization = document as ISupportInitialize;
  400. if (supportsInitialization != null)
  401. {
  402. supportsInitialization.BeginInit();
  403. }
  404. #endif
  405. // process any left over values that weren't passed to the creator
  406. foreach (var keyValuePair in values)
  407. {
  408. var elementName = keyValuePair.Key;
  409. var value = keyValuePair.Value;
  410. var memberMap = _classMap.GetMemberMapForElement(elementName);
  411. if (!memberMap.IsReadOnly)
  412. {
  413. memberMap.Setter.Invoke(document, value);
  414. }
  415. }
  416. #if NET45 || NETSTANDARD2_0
  417. if (supportsInitialization != null)
  418. {
  419. supportsInitialization.EndInit();
  420. }
  421. #endif
  422. return (TClass)document;
  423. }
  424. private void DeserializeExtraElementMember(
  425. BsonDeserializationContext context,
  426. object obj,
  427. string elementName,
  428. BsonMemberMap extraElementsMemberMap)
  429. {
  430. var bsonReader = context.Reader;
  431. if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
  432. {
  433. var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);
  434. if (extraElements == null)
  435. {
  436. extraElements = new BsonDocument();
  437. extraElementsMemberMap.Setter(obj, extraElements);
  438. }
  439. var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
  440. extraElements[elementName] = bsonValue;
  441. }
  442. else
  443. {
  444. var extraElements = (IDictionary<string, object>)extraElementsMemberMap.Getter(obj);
  445. if (extraElements == null)
  446. {
  447. if (extraElementsMemberMap.MemberType == typeof(IDictionary<string, object>))
  448. {
  449. extraElements = new Dictionary<string, object>();
  450. }
  451. else
  452. {
  453. extraElements = (IDictionary<string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType);
  454. }
  455. extraElementsMemberMap.Setter(obj, extraElements);
  456. }
  457. var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
  458. extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
  459. }
  460. }
  461. private void DeserializeExtraElementValue(
  462. BsonDeserializationContext context,
  463. Dictionary<string, object> values,
  464. string elementName,
  465. BsonMemberMap extraElementsMemberMap)
  466. {
  467. var bsonReader = context.Reader;
  468. if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
  469. {
  470. BsonDocument extraElements;
  471. object obj;
  472. if (values.TryGetValue(extraElementsMemberMap.ElementName, out obj))
  473. {
  474. extraElements = (BsonDocument)obj;
  475. }
  476. else
  477. {
  478. extraElements = new BsonDocument();
  479. values.Add(extraElementsMemberMap.ElementName, extraElements);
  480. }
  481. var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
  482. extraElements[elementName] = bsonValue;
  483. }
  484. else
  485. {
  486. IDictionary<string, object> extraElements;
  487. object obj;
  488. if (values.TryGetValue(extraElementsMemberMap.ElementName, out obj))
  489. {
  490. extraElements = (IDictionary<string, object>)obj;
  491. }
  492. else
  493. {
  494. if (extraElementsMemberMap.MemberType == typeof(IDictionary<string, object>))
  495. {
  496. extraElements = new Dictionary<string, object>();
  497. }
  498. else
  499. {
  500. extraElements = (IDictionary<string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType);
  501. }
  502. values.Add(extraElementsMemberMap.ElementName, extraElements);
  503. }
  504. var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
  505. extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
  506. }
  507. }
  508. private object DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
  509. {
  510. var bsonReader = context.Reader;
  511. try
  512. {
  513. return memberMap.GetSerializer().Deserialize(context);
  514. }
  515. catch (Exception ex)
  516. {
  517. var message = string.Format(
  518. "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message
  519. memberMap.MemberName, (memberMap.MemberInfo is FieldInfo) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message);
  520. throw new FormatException(message, ex);
  521. }
  522. }
  523. private void SerializeClass(BsonSerializationContext context, BsonSerializationArgs args, TClass document)
  524. {
  525. var bsonWriter = context.Writer;
  526. var remainingMemberMaps = _classMap.AllMemberMaps.ToList();
  527. bsonWriter.WriteStartDocument();
  528. var idMemberMap = _classMap.IdMemberMap;
  529. if (idMemberMap != null && args.SerializeIdFirst)
  530. {
  531. SerializeMember(context, document, idMemberMap);
  532. remainingMemberMaps.Remove(idMemberMap);
  533. }
  534. //var autoTimeStampMemberMap = _classMap.AutoTimeStampMemberMap;
  535. //if (autoTimeStampMemberMap != null)
  536. //{
  537. // SerializeNormalMember(context, document, autoTimeStampMemberMap);
  538. // remainingMemberMaps.Remove(autoTimeStampMemberMap);
  539. //}
  540. if (ShouldSerializeDiscriminator(args.NominalType))
  541. {
  542. SerializeDiscriminator(context, args.NominalType, document);
  543. }
  544. foreach (var memberMap in remainingMemberMaps)
  545. {
  546. SerializeMember(context, document, memberMap);
  547. }
  548. bsonWriter.WriteEndDocument();
  549. }
  550. private void SerializeExtraElements(BsonSerializationContext context, object obj, BsonMemberMap extraElementsMemberMap)
  551. {
  552. var bsonWriter = context.Writer;
  553. var extraElements = extraElementsMemberMap.Getter(obj);
  554. if (extraElements != null)
  555. {
  556. if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
  557. {
  558. var bsonDocument = (BsonDocument)extraElements;
  559. foreach (var element in bsonDocument)
  560. {
  561. bsonWriter.WriteName(element.Name);
  562. BsonValueSerializer.Instance.Serialize(context, element.Value);
  563. }
  564. }
  565. else
  566. {
  567. var dictionary = (IDictionary<string, object>)extraElements;
  568. foreach (var key in dictionary.Keys)
  569. {
  570. bsonWriter.WriteName(key);
  571. var value = dictionary[key];
  572. var bsonValue = BsonTypeMapper.MapToBsonValue(value);
  573. BsonValueSerializer.Instance.Serialize(context, bsonValue);
  574. }
  575. }
  576. }
  577. }
  578. private void SerializeDiscriminator(BsonSerializationContext context, Type nominalType, object obj)
  579. {
  580. var discriminatorConvention = _classMap.GetDiscriminatorConvention();
  581. if (discriminatorConvention != null)
  582. {
  583. var actualType = obj.GetType();
  584. var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
  585. if (discriminator != null)
  586. {
  587. context.Writer.WriteName(discriminatorConvention.ElementName);
  588. BsonValueSerializer.Instance.Serialize(context, discriminator);
  589. }
  590. }
  591. }
  592. private void SerializeMember(BsonSerializationContext context, object obj, BsonMemberMap memberMap)
  593. {
  594. if (memberMap != _classMap.ExtraElementsMemberMap)
  595. {
  596. SerializeNormalMember(context, obj, memberMap);
  597. }
  598. else
  599. {
  600. SerializeExtraElements(context, obj, memberMap);
  601. }
  602. }
  603. private void SerializeNormalMember(BsonSerializationContext context, object obj, BsonMemberMap memberMap)
  604. {
  605. var bsonWriter = context.Writer;
  606. var value = memberMap.Getter(obj);
  607. if (!memberMap.ShouldSerialize(obj, value))
  608. {
  609. return; // don't serialize member
  610. }
  611. bsonWriter.WriteName(memberMap.ElementName);
  612. memberMap.GetSerializer().Serialize(context, value);
  613. }
  614. private bool ShouldSerializeDiscriminator(Type nominalType)
  615. {
  616. return (nominalType != _classMap.ClassType || _classMap.DiscriminatorIsRequired || _classMap.HasRootClass) && !_classMap.IsAnonymous;
  617. }
  618. // nested classes
  619. // helper class that implements member map bit array helper functions
  620. private static class FastMemberMapHelper
  621. {
  622. public static uint[] GetBitArray(int memberCount)
  623. {
  624. var bitArrayOffset = memberCount & 31;
  625. var bitArrayLength = memberCount >> 5;
  626. if (bitArrayOffset == 0)
  627. {
  628. return new uint[bitArrayLength];
  629. }
  630. var bitArray = new uint[bitArrayLength + 1];
  631. bitArray[bitArrayLength] = ~0U << bitArrayOffset; // set unused bits to 1
  632. return bitArray;
  633. }
  634. // see http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
  635. // also returns 31 if no bits are set; caller must check this case
  636. public static int GetLeastSignificantBit(uint bitBlock)
  637. {
  638. var leastSignificantBit = 1;
  639. if ((bitBlock & 65535) == 0)
  640. {
  641. bitBlock >>= 16;
  642. leastSignificantBit |= 16;
  643. }
  644. if ((bitBlock & 255) == 0)
  645. {
  646. bitBlock >>= 8;
  647. leastSignificantBit |= 8;
  648. }
  649. if ((bitBlock & 15) == 0)
  650. {
  651. bitBlock >>= 4;
  652. leastSignificantBit |= 4;
  653. }
  654. if ((bitBlock & 3) == 0)
  655. {
  656. bitBlock >>= 2;
  657. leastSignificantBit |= 2;
  658. }
  659. return leastSignificantBit - (int)(bitBlock & 1);
  660. }
  661. }
  662. }
  663. }