Exemplo n.º 1
0
        /// <summary>
        /// The Deserialize function.
        /// </summary>
        /// <param name="context">The context parameter.</param>
        /// <param name="args">The arguments parameter.</param>
        /// <returns>The result of the deserialization of the element.</returns>
        public override List <ClientAddressDto> Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
        {
            context?.Reader.ReadStartArray();

            var result = new List <ClientAddressDto>();

            while (true)
            {
                try
                {
                    // this catch block only need to identify the end of the Array
                    context.Reader.ReadStartDocument();
                }
                catch (Exception)
                {
                    context.Reader.ReadEndArray();
                    break;
                }

                var street     = context.Reader.ReadString();
                var number     = context.Reader.ReadString();
                var postalCode = context.Reader.ReadString();

                result.Add(new ClientAddressDto(street, number, postalCode));

                context.Reader.ReadEndDocument();
            }

            return(result);
        }
Exemplo n.º 2
0
        public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            BsonReader bsonReader = (BsonReader)context.Reader;

            CalendarWrapper item = new CalendarWrapper();

            bsonReader.ReadStartDocument();
            item.Name = bsonReader.ReadString(ID);
            var binaryData = bsonReader.ReadBinaryData(CONTENT_STREAM);
            item.Calendar = (ICalendar)new BinaryFormatter().Deserialize(new MemoryStream(binaryData.Bytes));
            bsonReader.ReadEndDocument();

            return item;
        }
        public override bool Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            IBsonReader bsonReader = context.Reader;

            BsonType bsonType = bsonReader.GetCurrentBsonType();
            if (bsonType == BsonType.String)
            {
                string token = bsonReader.ReadString();
                return ParseBoolean.FromString(token);
            }
            else
            {
                return base.Deserialize(context, args);
            }
        }
 public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 {
     var bsonType = context.Reader.CurrentBsonType;
     switch (bsonType)
     {
         case BsonType.Null:
             context.Reader.ReadNull();
             return null;
         case BsonType.String:
             return context.Reader.ReadString();
         case BsonType.Int32:
             return context.Reader.ReadInt32().ToString();
         default:
             var message = string.Format("ZipCodeSerializer expects to find a String or an Int32, not a {0}.", bsonType);
             throw new BsonSerializationException(message);
     }
 }
        public object Deserialize( BsonDeserializationContext context, BsonDeserializationArgs args )
        {
            var bsonReader = context.Reader;
            BsonType bsonType = bsonReader.CurrentBsonType;

            object result;

            if( bsonType == BsonType.Null )
            {
                bsonReader.ReadNull();
                result = null;
            }
            else
            {
                if( bsonType == BsonType.Document )
                {
                    var dictionary = new DynamicDictionary();

                    bsonReader.ReadStartDocument();

                    IDiscriminatorConvention valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention( typeof( object ) );

                    while( bsonReader.ReadBsonType() != BsonType.EndOfDocument )
                    {
                        string key = bsonReader.ReadName();
                        Type valueType = valueDiscriminatorConvention.GetActualType( bsonReader, typeof( object ) );
                        IBsonSerializer valueSerializer = BsonSerializer.LookupSerializer( valueType );
                        object value = valueSerializer.Deserialize( context );

                        if( key != "_t" )
                        {
                            dictionary.Add( key.Replace( '\x03', '.' ), value );
                        }
                    }
                    bsonReader.ReadEndDocument();
                    result = dictionary;
                }
                else
                {
                    string message = string.Format( "Can't deserialize a {0} from BsonType {1}.", context.Reader.CurrentBsonType, bsonType );
                    throw new BsonException( message );
                }
            }

            return result;
        }
            public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
            {
                var bsonType = context.Reader.CurrentBsonType;
                switch (bsonType)
                {
                    case BsonType.Null:
                        context.Reader.ReadNull();
                        return null;

                    case BsonType.String:
                        var value = context.Reader.ReadString();
                        if (!string.IsNullOrEmpty(value))
                        {
                            value = string.Concat(char.ToUpperInvariant(value[0]), value.Substring(1));
                            var enumValue = Enum.Parse(ValueType, value);
                            return enumValue;
                        }
                        return value;

                    default:
                        return baseSerializer.Deserialize(context, args);
                }
            }
 private BsonValue DeserializeBsonValue(BsonDeserializationContext context)
 {
     var bsonReader = context.Reader;
     switch (bsonReader.GetCurrentBsonType())
     {
         case BsonType.Array: return DeserializeRawBsonArray(bsonReader);
         case BsonType.Document: return DeserializeRawBsonDocument(bsonReader);
         default: return BsonValueSerializer.Instance.Deserialize(context);
     }
 }
            // constructors
            internal Builder(BsonDeserializationContext other, IBsonReader reader)
            {
                if (reader == null)
                {
                    throw new ArgumentNullException("reader");
                }

                _reader = reader;
                if (other != null)
                {
                    _allowDuplicateElementNames = other.AllowDuplicateElementNames;
                    _dynamicArraySerializer = other.DynamicArraySerializer;
                    _dynamicDocumentSerializer = other.DynamicDocumentSerializer;
                }
                else
                {
                    _dynamicArraySerializer = BsonDefaults.DynamicArraySerializer;
                    _dynamicDocumentSerializer = BsonDefaults.DynamicDocumentSerializer;
                }
            }
Exemplo n.º 9
0
        /// <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);

#if NET452
            ISupportInitialize supportsInitialization = null;
#endif
            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();

#if NET452
                supportsInitialization = document as ISupportInitialize;
                if (supportsInitialization != null)
                {
                    supportsInitialization.BeginInit();
                }
#endif
            }

            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 NET452
                if (supportsInitialization != null)
                {
                    supportsInitialization.EndInit();
                }
#endif

                return(document);
            }
            else
            {
                return(CreateInstanceUsingCreator(values));
            }
        }
Exemplo n.º 10
0
 public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 {
     return Enum.Parse(ValueType, context.Reader.ReadString());
 }
 public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 {
     throw new NotImplementedException();
 }
 public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 {
     var key = context.Reader.ReadString();
     var gr = GrainReference.FromKeyString(key);
     return gr;
 }