Exemplo n.º 1
0
        /// <summary>
        /// Creates an instance of the <see cref="SerializationObject" /> which represents a failed deserialized value.
        /// </summary>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="memberGroup">Type of the member.</param>
        /// <param name="memberName">Name of the member.</param>
        /// <returns>SerializationObject.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="modelType" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="memberName" /> is <c>null</c> or whitespace.</exception>
        public static SerializationObject FailedToDeserialize(Type modelType, SerializationMemberGroup memberGroup, string memberName)
        {
            Argument.IsNotNull("modelType", modelType);
            Argument.IsNotNullOrWhitespace("memberName", memberName);

            var obj = new SerializationObject(modelType, memberGroup, memberName, null);

            obj.IsSuccessful = false;

            return(obj);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an instance of the <see cref="SerializationObject" /> which represents a failed deserialized value.
        /// </summary>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="memberGroup">Type of the member.</param>
        /// <param name="memberName">Name of the member.</param>
        /// <returns>SerializationObject.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="modelType" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="memberName" /> is <c>null</c> or whitespace.</exception>
        public static SerializationObject FailedToDeserialize(Type modelType, SerializationMemberGroup memberGroup, string memberName)
        {
            Argument.IsNotNull("modelType", modelType);
            Argument.IsNotNullOrWhitespace("memberName", memberName);

            var obj = new SerializationObject(modelType, memberGroup, memberName, null);
            obj.IsSuccessful = false;

            return obj;            
        }
        /// <summary>
        /// Ends member deserialization by invoking all the right events and running the modifiers.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="member">The member that has been deserialized.</param>
        /// <param name="serializationObject">Result of the member deserialization.</param>
        /// <param name="serializerModifiers">The serializer modifiers.</param>
        protected virtual MemberValue EndMemberDeserialization(ISerializationContext <TSerializationContextInfo> context, MemberValue member,
                                                               SerializationObject serializationObject, IEnumerable <ISerializerModifier> serializerModifiers)
        {
            if (!serializationObject.IsSuccessful)
            {
                return(null);
            }

            // Note that we need to sync the member values every time
            var memberValue = new MemberValue(member.MemberGroup, member.ModelType, member.MemberType, member.Name,
                                              member.NameForSerialization, serializationObject.MemberValue);

            if (memberValue.MemberGroup == SerializationMemberGroup.Dictionary ||
                ShouldSerializeAsDictionary(member))
            {
                var targetDictionary = TypeFactory.CreateInstance(member.MemberType) as IDictionary;
                if (targetDictionary is null)
                {
                    throw Log.ErrorAndCreateException <NotSupportedException>("'{0}' seems to be a dictionary, but target model cannot be updated because it does not implement IDictionary",
                                                                              context.ModelTypeName);
                }

                var enumerable = memberValue.Value as List <SerializableKeyValuePair>;
                if (enumerable != null)
                {
                    foreach (var item in enumerable)
                    {
                        targetDictionary.Add(item.Key, item.Value);
                    }
                }
                else
                {
                    var sourceDictionary = memberValue.Value as IDictionary;
                    if (sourceDictionary != null)
                    {
                        foreach (var key in sourceDictionary.Keys)
                        {
                            targetDictionary.Add(key, sourceDictionary[key]);
                        }
                    }
                }

                member.Value = targetDictionary;
            }
            else if (memberValue.MemberGroup == SerializationMemberGroup.Collection)
            {
                var sourceCollection = memberValue.Value as IEnumerable;

                if (member.MemberType.IsArrayEx())
                {
                    var elementType = member.MemberType.GetElementTypeEx();
                    member.Value = sourceCollection.ToArray(elementType);
                }
                else
                {
                    var targetCollection = TypeFactory.CreateInstance(member.MemberType) as IList;
                    if (targetCollection is null)
                    {
                        throw Log.ErrorAndCreateException <NotSupportedException>("'{0}' seems to be a collection, but target model cannot be updated because it does not implement IList",
                                                                                  context.ModelTypeName);
                    }

                    if (sourceCollection != null)
                    {
                        foreach (var item in sourceCollection)
                        {
                            targetCollection.Add(item);
                        }
                    }

                    member.Value = targetCollection;
                }
            }
            else
            {
                member.Value = memberValue.Value;
            }

            foreach (var serializerModifier in serializerModifiers)
            {
                serializerModifier.DeserializeMember(context, member);
                memberValue.Value = member.Value;
            }

            AfterDeserializeMember(context, member);
            memberValue.Value = member.Value;

            DeserializedMember?.Invoke(this, new MemberSerializationEventArgs(context, member));
            memberValue.Value = member.Value;

            return(memberValue);
        }