示例#1
0
        /// <summary>
        ///   Deserializes an object from a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="data">Dictionary which contains the object data.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(DictionarySerializationContext context, Dictionary <string, object> data)
        {
            if (!data.ContainsKey(DataCount))
            {
                throw new ArgumentException(string.Format("List property not specified: {0}", DataCount));
            }

            if (!data.ContainsKey(DataType))
            {
                throw new ArgumentException(string.Format("List property not specified: {0}", DataType));
            }

            int    count          = Convert.ToInt32(data[DataCount]);
            string itemTypeString = (string)data[DataType];
            Type   itemType       = ReflectionUtils.FindType(itemTypeString);

            if (itemType == null)
            {
                throw new SerializationException(string.Format("Item type '{0}' not found", itemTypeString));
            }

            IList list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(itemType));

            bool typeSealed = itemType.IsSealed();

            for (int i = 0; i < count; i++)
            {
                object valueData = data[i.ToString(CultureInfo.InvariantCulture)];

                object value;
                if (valueData == null)
                {
                    value = null;
                }
                else if (typeSealed)
                {
                    value = context.Deserialize(itemType, valueData);
                }
                else
                {
                    ValueWithType valueWithType = context.Deserialize(typeof(ValueWithType), valueData) as ValueWithType;
                    value = valueWithType.Value;
                }

                list.Add(value);
            }

            return(list);
        }
        /// <summary>
        ///   Deserializes an object from a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="data">Dictionary which contains the object data.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(DictionarySerializationContext context, Dictionary <string, object> data)
        {
            int    count          = Convert.ToInt32(data[DataCount]);
            string itemTypeString = (string)data[DataType];
            Type   itemType       = ReflectionUtils.FindType(itemTypeString);

            if (itemType == null)
            {
                throw new SerializationException(string.Format("Item type '{0}' not found", itemTypeString));
            }

            Type   genericType = typeof(Stack <>).MakeGenericType(itemType);
            object stack       = Activator.CreateInstance(genericType);

            bool       typeSealed = itemType.IsSealed();
            MethodInfo pushMethod = genericType.GetMethod("Push");

            for (int i = count - 1; i >= 0; --i)
            {
                object valueData = data[i.ToString(CultureInfo.InvariantCulture)];
                object value;
                if (valueData == null)
                {
                    value = null;
                }
                else if (typeSealed)
                {
                    value = context.Deserialize(itemType, valueData);
                }
                else
                {
                    ValueWithType valueWithType = context.Deserialize(typeof(ValueWithType), valueData) as ValueWithType;
                    value = valueWithType.Value;
                }

                pushMethod.Invoke(stack, new[] { value });
            }

            return(stack);
        }
示例#3
0
        /// <summary>
        ///   Reads the object from the specified dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="data">Object to read.</param>
        /// <returns>Read object.</returns>
        public object Deserialize(DictionarySerializationContext context, Dictionary <string, object> data)
        {
            string itemTypeString = (string)data[DataType];
            Type   itemType       = ReflectionUtils.FindType(itemTypeString);

            if (itemType == null)
            {
                throw new SerializationException(string.Format("Item type '{0}' not found", itemTypeString));
            }

            Type        genericType = typeof(Dictionary <,>).MakeGenericType(typeof(string), itemType);
            IDictionary dictionary  = (IDictionary)Activator.CreateInstance(genericType);

            bool typeSealed = itemType.IsSealed();
            Dictionary <string, object> pairs = (Dictionary <string, object>)data[DataPairs];

            foreach (KeyValuePair <string, object> pair in pairs)
            {
                object valueData = pair.Value;
                object value;
                if (valueData == null)
                {
                    value = null;
                }
                else if (typeSealed)
                {
                    value = context.Deserialize(itemType, valueData);
                }
                else
                {
                    ValueWithType valueWithType = context.Deserialize(typeof(ValueWithType), valueData) as ValueWithType;
                    value = valueWithType.Value;
                }

                dictionary.Add(pair.Key, value);
            }

            return(dictionary);
        }
        /// <summary>
        ///   Deserializes an object from a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="data">Dictionary which contains the object data.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(DictionarySerializationContext context, Dictionary <string, object> data)
        {
            ValueWithType valueWithType = new ValueWithType();

            object typeFullName;
            object value = data[DataValue];

            if (data.TryGetValue(DataType, out typeFullName))
            {
                valueWithType.TypeFullName = (string)typeFullName;
                Type type = valueWithType.Type;
                valueWithType.Value = context.Deserialize(type, value);
            }
            else
            {
                valueWithType.Value = value;
                valueWithType.Type  = value.GetType();
            }

            return(valueWithType);
        }