| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- /* Copyright 2010-present MongoDB Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization.Serializers;
- namespace MongoDB.Bson.Serialization
- {
- /// <summary>
- /// Represents a serializer for a class map.
- /// </summary>
- /// <typeparam name="TClass">The type of the class.</typeparam>
- public class BsonClassMapSerializer<TClass> : SerializerBase<TClass>, IBsonIdProvider, IBsonDocumentSerializer, IBsonPolymorphicSerializer
- {
- // private fields
- private BsonClassMap _classMap;
- // constructors
- /// <summary>
- /// Initializes a new instance of the BsonClassMapSerializer class.
- /// </summary>
- /// <param name="classMap">The class map.</param>
- public BsonClassMapSerializer(BsonClassMap classMap)
- {
- if (classMap == null)
- {
- throw new ArgumentNullException("classMap");
- }
- if (classMap.ClassType != typeof(TClass))
- {
- var message = string.Format("Must be a BsonClassMap for the type {0}.", typeof(TClass));
- throw new ArgumentException(message, "classMap");
- }
- if (!classMap.IsFrozen)
- {
- throw new ArgumentException("Class map is not frozen.", nameof(classMap));
- }
- _classMap = classMap;
- }
- // public properties
- /// <summary>
- /// Gets a value indicating whether this serializer's discriminator is compatible with the object serializer.
- /// </summary>
- /// <value>
- /// <c>true</c> if this serializer's discriminator is compatible with the object serializer; otherwise, <c>false</c>.
- /// </value>
- public bool IsDiscriminatorCompatibleWithObjectSerializer
- {
- get { return true; }
- }
- // public methods
- /// <summary>
- /// Deserializes a value.
- /// </summary>
- /// <param name="context">The deserialization context.</param>
- /// <param name="args">The deserialization args.</param>
- /// <returns>A deserialized value.</returns>
- public override TClass Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
- {
- var bsonReader = context.Reader;
- if (_classMap.ClassType.GetTypeInfo().IsValueType)
- {
- var message = string.Format("Value class {0} cannot be deserialized.", _classMap.ClassType.FullName);
- throw new BsonSerializationException(message);
- }
- if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
- {
- bsonReader.ReadNull();
- return default(TClass);
- }
- else
- {
- var discriminatorConvention = _classMap.GetDiscriminatorConvention();
- var actualType = discriminatorConvention.GetActualType(bsonReader, args.NominalType);
- if (actualType == typeof(TClass))
- {
- return DeserializeClass(context);
- }
- else
- {
- var serializer = BsonSerializer.LookupSerializer(actualType);
- return (TClass)serializer.Deserialize(context);
- }
- }
- }
- /// <summary>
- /// Deserializes a value.
- /// </summary>
- /// <param name="context">The deserialization context.</param>
- /// <returns>A deserialized value.</returns>
- public TClass DeserializeClass(BsonDeserializationContext context)
- {
- var bsonReader = context.Reader;
- var bsonType = bsonReader.GetCurrentBsonType();
- if (bsonType != BsonType.Document)
- {
- var message = string.Format(
- "Expected a nested document representing the serialized form of a {0} value, but found a value of type {1} instead.",
- typeof(TClass).FullName, bsonType);
- throw new FormatException(message);
- }
- Dictionary<string, object> values = null;
- var document = default(TClass);
- ISupportInitialize supportsInitialization = null;
- if (_classMap.HasCreatorMaps)
- {
- // for creator-based deserialization we first gather the values in a dictionary and then call a matching creator
- values = new Dictionary<string, object>();
- }
- else
- {
- // for mutable classes we deserialize the values directly into the result object
- document = (TClass)_classMap.CreateInstance();
- supportsInitialization = document as ISupportInitialize;
- if (supportsInitialization != null)
- {
- supportsInitialization.BeginInit();
- }
- }
- var discriminatorConvention = _classMap.GetDiscriminatorConvention();
- var allMemberMaps = _classMap.AllMemberMaps;
- var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex;
- var memberMapBitArray = FastMemberMapHelper.GetBitArray(allMemberMaps.Count);
- bsonReader.ReadStartDocument();
- var elementTrie = _classMap.ElementTrie;
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- var trieDecoder = new TrieNameDecoder<int>(elementTrie);
- var elementName = bsonReader.ReadName(trieDecoder);
- if (trieDecoder.Found)
- {
- var memberMapIndex = trieDecoder.Value;
- var memberMap = allMemberMaps[memberMapIndex];
- if (memberMapIndex != extraElementsMemberMapIndex)
- {
- if (document != null)
- {
- if (memberMap.IsReadOnly)
- {
- bsonReader.SkipValue();
- }
- else
- {
- var value = DeserializeMemberValue(context, memberMap);
- memberMap.Setter(document, value);
- }
- }
- else
- {
- var value = DeserializeMemberValue(context, memberMap);
- values[elementName] = value;
- }
- }
- else
- {
- if (document != null)
- {
- DeserializeExtraElementMember(context, document, elementName, memberMap);
- }
- else
- {
- DeserializeExtraElementValue(context, values, elementName, memberMap);
- }
- }
- memberMapBitArray[memberMapIndex >> 5] |= 1U << (memberMapIndex & 31);
- }
- else
- {
- if (elementName == discriminatorConvention.ElementName)
- {
- bsonReader.SkipValue(); // skip over discriminator
- continue;
- }
- if (extraElementsMemberMapIndex >= 0)
- {
- var extraElementsMemberMap = _classMap.ExtraElementsMemberMap;
- if (document != null)
- {
- DeserializeExtraElementMember(context, document, elementName, extraElementsMemberMap);
- }
- else
- {
- DeserializeExtraElementValue(context, values, elementName, extraElementsMemberMap);
- }
- memberMapBitArray[extraElementsMemberMapIndex >> 5] |= 1U << (extraElementsMemberMapIndex & 31);
- }
- else if (_classMap.IgnoreExtraElements)
- {
- bsonReader.SkipValue();
- }
- else
- {
- var message = string.Format(
- "Element '{0}' does not match any field or property of class {1}.",
- elementName, _classMap.ClassType.FullName);
- throw new FormatException(message);
- }
- }
- }
- bsonReader.ReadEndDocument();
- // check any members left over that we didn't have elements for (in blocks of 32 elements at a time)
- for (var bitArrayIndex = 0; bitArrayIndex < memberMapBitArray.Length; ++bitArrayIndex)
- {
- var memberMapIndex = bitArrayIndex << 5;
- var memberMapBlock = ~memberMapBitArray[bitArrayIndex]; // notice that bits are flipped so 1's are now the missing elements
- // work through this memberMapBlock of 32 elements
- while (true)
- {
- // examine missing elements (memberMapBlock is shifted right as we work through the block)
- for (; (memberMapBlock & 1) != 0; ++memberMapIndex, memberMapBlock >>= 1)
- {
- var memberMap = allMemberMaps[memberMapIndex];
- if (memberMap.IsReadOnly)
- {
- continue;
- }
- if (memberMap.IsRequired)
- {
- var fieldOrProperty = (memberMap.MemberInfo is FieldInfo) ? "field" : "property";
- var message = string.Format(
- "Required element '{0}' for {1} '{2}' of class {3} is missing.",
- memberMap.ElementName, fieldOrProperty, memberMap.MemberName, _classMap.ClassType.FullName);
- throw new FormatException(message);
- }
- if (document != null)
- {
- memberMap.ApplyDefaultValue(document);
- }
- else if (memberMap.IsDefaultValueSpecified && !memberMap.IsReadOnly)
- {
- values[memberMap.ElementName] = memberMap.DefaultValue;
- }
- }
- if (memberMapBlock == 0)
- {
- break;
- }
- // skip ahead to the next missing element
- var leastSignificantBit = FastMemberMapHelper.GetLeastSignificantBit(memberMapBlock);
- memberMapIndex += leastSignificantBit;
- memberMapBlock >>= leastSignificantBit;
- }
- }
- if (document != null)
- {
- if (supportsInitialization != null)
- {
- supportsInitialization.EndInit();
- }
- return document;
- }
- else
- {
- return CreateInstanceUsingCreator(values);
- }
- }
- /// <summary>
- /// Gets the document Id.
- /// </summary>
- /// <param name="document">The document.</param>
- /// <param name="id">The Id.</param>
- /// <param name="idNominalType">The nominal type of the Id.</param>
- /// <param name="idGenerator">The IdGenerator for the Id type.</param>
- /// <returns>True if the document has an Id.</returns>
- public bool GetDocumentId(
- object document,
- out object id,
- out Type idNominalType,
- out IIdGenerator idGenerator)
- {
- var idMemberMap = _classMap.IdMemberMap;
- if (idMemberMap != null)
- {
- id = idMemberMap.Getter(document);
- idNominalType = idMemberMap.MemberType;
- idGenerator = idMemberMap.IdGenerator;
- return true;
- }
- else
- {
- id = null;
- idNominalType = null;
- idGenerator = null;
- return false;
- }
- }
- /// <summary>
- /// Tries to get the serialization info for a member.
- /// </summary>
- /// <param name="memberName">Name of the member.</param>
- /// <param name="serializationInfo">The serialization information.</param>
- /// <returns>
- /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
- /// </returns>
- public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
- {
- foreach (var memberMap in _classMap.AllMemberMaps)
- {
- if (memberMap.MemberName == memberName)
- {
- var elementName = memberMap.ElementName;
- var serializer = memberMap.GetSerializer();
- serializationInfo = new BsonSerializationInfo(elementName, serializer, serializer.ValueType);
- return true;
- }
- }
- serializationInfo = null;
- return false;
- }
- /// <summary>
- /// Serializes a value.
- /// </summary>
- /// <param name="context">The serialization context.</param>
- /// <param name="args">The serialization args.</param>
- /// <param name="value">The object.</param>
- public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
- {
- var bsonWriter = context.Writer;
- if (value == null)
- {
- bsonWriter.WriteNull();
- }
- else
- {
- var actualType = value.GetType();
- if (actualType == typeof(TClass))
- {
- SerializeClass(context, args, value);
- }
- else
- {
- var serializer = BsonSerializer.LookupSerializer(actualType);
- serializer.Serialize(context, args, value);
- }
- }
- }
- /// <summary>
- /// Sets the document Id.
- /// </summary>
- /// <param name="document">The document.</param>
- /// <param name="id">The Id.</param>
- public void SetDocumentId(object document, object id)
- {
- var documentType = document.GetType();
- var documentTypeInfo = documentType.GetTypeInfo();
- if (documentTypeInfo.IsValueType)
- {
- var message = string.Format("SetDocumentId cannot be used with value type {0}.", documentType.FullName);
- throw new BsonSerializationException(message);
- }
- var idMemberMap = _classMap.IdMemberMap;
- if (idMemberMap != null)
- {
- idMemberMap.Setter(document, id);
- }
- else
- {
- var message = string.Format("Class {0} has no Id member.", document.GetType().FullName);
- throw new InvalidOperationException(message);
- }
- }
- // private methods
- private BsonCreatorMap ChooseBestCreator(Dictionary<string, object> values)
- {
- // there's only one selector for now, but there might be more in the future (possibly even user provided)
- var selector = new MostArgumentsCreatorSelector();
- var creatorMap = selector.SelectCreator(_classMap, values);
- if (creatorMap == null)
- {
- throw new BsonSerializationException("No matching creator found.");
- }
- return creatorMap;
- }
- private TClass CreateInstanceUsingCreator(Dictionary<string, object> values)
- {
- var creatorMap = ChooseBestCreator(values);
- var document = creatorMap.CreateInstance(values); // removes values consumed
- var supportsInitialization = document as ISupportInitialize;
- if (supportsInitialization != null)
- {
- supportsInitialization.BeginInit();
- }
-
- // process any left over values that weren't passed to the creator
- foreach (var keyValuePair in values)
- {
- var elementName = keyValuePair.Key;
- var value = keyValuePair.Value;
- var memberMap = _classMap.GetMemberMapForElement(elementName);
- if (!memberMap.IsReadOnly)
- {
- memberMap.Setter.Invoke(document, value);
- }
- }
- if (supportsInitialization != null)
- {
- supportsInitialization.EndInit();
- }
- return (TClass)document;
- }
- private void DeserializeExtraElementMember(
- BsonDeserializationContext context,
- object obj,
- string elementName,
- BsonMemberMap extraElementsMemberMap)
- {
- var bsonReader = context.Reader;
- if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
- {
- var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);
- if (extraElements == null)
- {
- extraElements = new BsonDocument();
- extraElementsMemberMap.Setter(obj, extraElements);
- }
- var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
- extraElements[elementName] = bsonValue;
- }
- else
- {
- var extraElements = (IDictionary<string, object>)extraElementsMemberMap.Getter(obj);
- if (extraElements == null)
- {
- if (extraElementsMemberMap.MemberType == typeof(IDictionary<string, object>))
- {
- extraElements = new Dictionary<string, object>();
- }
- else
- {
- extraElements = (IDictionary<string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType);
- }
- extraElementsMemberMap.Setter(obj, extraElements);
- }
- var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
- extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
- }
- }
- private void DeserializeExtraElementValue(
- BsonDeserializationContext context,
- Dictionary<string, object> values,
- string elementName,
- BsonMemberMap extraElementsMemberMap)
- {
- var bsonReader = context.Reader;
- if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
- {
- BsonDocument extraElements;
- object obj;
- if (values.TryGetValue(extraElementsMemberMap.ElementName, out obj))
- {
- extraElements = (BsonDocument)obj;
- }
- else
- {
- extraElements = new BsonDocument();
- values.Add(extraElementsMemberMap.ElementName, extraElements);
- }
- var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
- extraElements[elementName] = bsonValue;
- }
- else
- {
- IDictionary<string, object> extraElements;
- object obj;
- if (values.TryGetValue(extraElementsMemberMap.ElementName, out obj))
- {
- extraElements = (IDictionary<string, object>)obj;
- }
- else
- {
- if (extraElementsMemberMap.MemberType == typeof(IDictionary<string, object>))
- {
- extraElements = new Dictionary<string, object>();
- }
- else
- {
- extraElements = (IDictionary<string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType);
- }
- values.Add(extraElementsMemberMap.ElementName, extraElements);
- }
- var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
- extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
- }
- }
- private object DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
- {
- var bsonReader = context.Reader;
- try
- {
- return memberMap.GetSerializer().Deserialize(context);
- }
- catch (Exception ex)
- {
- var message = string.Format(
- "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message
- memberMap.MemberName, (memberMap.MemberInfo is FieldInfo) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message);
- throw new FormatException(message, ex);
- }
- }
- private void SerializeClass(BsonSerializationContext context, BsonSerializationArgs args, TClass document)
- {
- var bsonWriter = context.Writer;
- var remainingMemberMaps = _classMap.AllMemberMaps.ToList();
- bsonWriter.WriteStartDocument();
- var idMemberMap = _classMap.IdMemberMap;
- if (idMemberMap != null && args.SerializeIdFirst)
- {
- SerializeMember(context, document, idMemberMap);
- remainingMemberMaps.Remove(idMemberMap);
- }
- //var autoTimeStampMemberMap = _classMap.AutoTimeStampMemberMap;
- //if (autoTimeStampMemberMap != null)
- //{
- // SerializeNormalMember(context, document, autoTimeStampMemberMap);
- // remainingMemberMaps.Remove(autoTimeStampMemberMap);
- //}
- if (ShouldSerializeDiscriminator(args.NominalType))
- {
- SerializeDiscriminator(context, args.NominalType, document);
- }
- foreach (var memberMap in remainingMemberMaps)
- {
- SerializeMember(context, document, memberMap);
- }
- bsonWriter.WriteEndDocument();
- }
- private void SerializeExtraElements(BsonSerializationContext context, object obj, BsonMemberMap extraElementsMemberMap)
- {
- var bsonWriter = context.Writer;
- var extraElements = extraElementsMemberMap.Getter(obj);
- if (extraElements != null)
- {
- if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
- {
- var bsonDocument = (BsonDocument)extraElements;
- foreach (var element in bsonDocument)
- {
- bsonWriter.WriteName(element.Name);
- BsonValueSerializer.Instance.Serialize(context, element.Value);
- }
- }
- else
- {
- var dictionary = (IDictionary<string, object>)extraElements;
- foreach (var key in dictionary.Keys)
- {
- bsonWriter.WriteName(key);
- var value = dictionary[key];
- var bsonValue = BsonTypeMapper.MapToBsonValue(value);
- BsonValueSerializer.Instance.Serialize(context, bsonValue);
- }
- }
- }
- }
- private void SerializeDiscriminator(BsonSerializationContext context, Type nominalType, object obj)
- {
- var discriminatorConvention = _classMap.GetDiscriminatorConvention();
- if (discriminatorConvention != null)
- {
- var actualType = obj.GetType();
- var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
- if (discriminator != null)
- {
- context.Writer.WriteName(discriminatorConvention.ElementName);
- BsonValueSerializer.Instance.Serialize(context, discriminator);
- }
- }
- }
- private void SerializeMember(BsonSerializationContext context, object obj, BsonMemberMap memberMap)
- {
- if (memberMap != _classMap.ExtraElementsMemberMap)
- {
- SerializeNormalMember(context, obj, memberMap);
- }
- else
- {
- SerializeExtraElements(context, obj, memberMap);
- }
- }
- private void SerializeNormalMember(BsonSerializationContext context, object obj, BsonMemberMap memberMap)
- {
- var bsonWriter = context.Writer;
- var value = memberMap.Getter(obj);
- if (!memberMap.ShouldSerialize(obj, value))
- {
- return; // don't serialize member
- }
- bsonWriter.WriteName(memberMap.ElementName);
- memberMap.GetSerializer().Serialize(context, value);
- }
- private bool ShouldSerializeDiscriminator(Type nominalType)
- {
- return (nominalType != _classMap.ClassType || _classMap.DiscriminatorIsRequired || _classMap.HasRootClass) && !_classMap.IsAnonymous;
- }
- // nested classes
- // helper class that implements member map bit array helper functions
- private static class FastMemberMapHelper
- {
- public static uint[] GetBitArray(int memberCount)
- {
- var bitArrayOffset = memberCount & 31;
- var bitArrayLength = memberCount >> 5;
- if (bitArrayOffset == 0)
- {
- return new uint[bitArrayLength];
- }
- var bitArray = new uint[bitArrayLength + 1];
- bitArray[bitArrayLength] = ~0U << bitArrayOffset; // set unused bits to 1
- return bitArray;
- }
- // see http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
- // also returns 31 if no bits are set; caller must check this case
- public static int GetLeastSignificantBit(uint bitBlock)
- {
- var leastSignificantBit = 1;
- if ((bitBlock & 65535) == 0)
- {
- bitBlock >>= 16;
- leastSignificantBit |= 16;
- }
- if ((bitBlock & 255) == 0)
- {
- bitBlock >>= 8;
- leastSignificantBit |= 8;
- }
- if ((bitBlock & 15) == 0)
- {
- bitBlock >>= 4;
- leastSignificantBit |= 4;
- }
- if ((bitBlock & 3) == 0)
- {
- bitBlock >>= 2;
- leastSignificantBit |= 2;
- }
- return leastSignificantBit - (int)(bitBlock & 1);
- }
- }
- }
- }
|