예제 #1
0
        private IList DeserializeList()
        {
            int    count          = this.reader.ReadInt32();
            string itemTypeString = this.reader.ReadString();
            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));

            for (int i = 0; i < count; i++)
            {
                if (itemType.IsSealed())
                {
                    list.Add(this.Deserialize(itemType));
                }
                else
                {
                    ValueWithType valueWithType = this.DeserializeValueWithType();
                    list.Add(valueWithType.Value);
                }
            }

            return(list);
        }
예제 #2
0
        private void SerializeValueWithType(ValueWithType valueWithType)
        {
            string typeName = SystemExtensions.RemoveAssemblyInfo(valueWithType.TypeFullName);

            this.writer.Write(typeName);
            this.Serialize(valueWithType.Value, valueWithType.Type);
        }
예제 #3
0
        private object DeserializeArray()
        {
            int    count          = this.reader.ReadInt32();
            string itemTypeString = this.reader.ReadString();
            Type   itemType       = ReflectionUtils.FindType(itemTypeString);

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

            Array array = Array.CreateInstance(itemType, count);

            for (int i = 0; i < count; i++)
            {
                if (itemType.IsSealed())
                {
                    array.SetValue(this.Deserialize(itemType), i);
                }
                else
                {
                    ValueWithType valueWithType = this.DeserializeValueWithType();
                    array.SetValue(valueWithType.Value, i);
                }
            }

            return(array);
        }
예제 #4
0
        private void PopulateValues(IDictionary <string, IList <string> > from, List <KeyValue> to)
        {
            IEnumerator ide = from.GetEnumerator();

            foreach (var item in from)
            {
                var keyValue = new KeyValue {
                    key = item.Key.ToLower()
                };

                var set = item.Value as List <string>;
                if (set != null)
                {
                    foreach (string value in set)
                    {
                        var valueWithType = new ValueWithType {
                            value = value
                        };
                        keyValue.value.Add(valueWithType);
                    }
                }

                to.Add(keyValue);
            }
        }
예제 #5
0
        private void PopulateValues(IDictionary from, System.Collections.Generic.List <KeyValue> to)
        {
            IDictionaryEnumerator ide           = from.GetEnumerator();
            ValueWithType         valueWithType = null;
            KeyValue keyValue = null;

            while (ide.MoveNext())
            {
                keyValue     = new KeyValue();
                keyValue.key = ide.Key.ToString();

                if (ide.Value is ArrayList)
                {
                    ArrayList list = (ArrayList)ide.Value;
                    foreach (object value in list)
                    {
                        valueWithType       = new ValueWithType();
                        valueWithType.value = GetValueString(value);
                        valueWithType.type  = value.GetType().FullName;

                        keyValue.value.Add(valueWithType);
                    }
                }
                else
                {
                    valueWithType       = new ValueWithType();
                    valueWithType.value = GetValueString(ide.Value);
                    valueWithType.type  = ide.Value.GetType().FullName;

                    keyValue.value.Add(valueWithType);
                }

                to.Add(keyValue);
            }
        }
예제 #6
0
        private ValueWithType DeserializeValueWithType()
        {
            ValueWithType valueWithType = new ValueWithType();

            valueWithType.TypeFullName = this.reader.ReadString();
            valueWithType.Value        = this.Deserialize(valueWithType.Type);

            return(valueWithType);
        }
예제 #7
0
        public static void PopulateValues(IDictionary from, System.Collections.Generic.List <KeyValue> to)
        {
            IDictionaryEnumerator ide           = from.GetEnumerator();
            ValueWithType         valueWithType = null;
            KeyValue keyValue = null;

            while (ide.MoveNext())
            {
                keyValue     = new KeyValue();
                keyValue.key = ide.Key.ToString();
                if (ide.Value == null)
                {
                    throw new ArgumentException("NCache query does not support null values");
                }
                if (ide.Value is ArrayList)
                {
                    ArrayList list = (ArrayList)ide.Value;
                    foreach (object value in list)
                    {
                        if (value == null)
                        {
                            throw new ArgumentNullException("NCache query does not support null values. ",
                                                            (System.Exception)null);
                        }
                        Type type = value.GetType();

                        if (!(CommandHelper.IsIndexable(type)))
                        {
                            throw new ArgumentException("The provided type is not indexable. ", type.Name);
                        }
                        valueWithType       = new ValueWithType();
                        valueWithType.value = GetValueString(value);
                        valueWithType.type  = value.GetType().FullName;

                        keyValue.value.Add(valueWithType);
                    }
                }
                else
                {
                    Type type = ide.Value.GetType();
                    if (!(CommandHelper.IsIndexable(type)))
                    {
                        throw new ArgumentException("The provided type is not indexable. ", type.Name);
                    }
                    valueWithType       = new ValueWithType();
                    valueWithType.value = GetValueString(ide.Value);
                    valueWithType.type  = ide.Value.GetType().FullName;
                    keyValue.value.Add(valueWithType);
                }

                to.Add(keyValue);
            }
        }
예제 #8
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);
        }
예제 #9
0
        private IDictionary DeserializeDictionary()
        {
            int count = this.reader.ReadInt32();

            // Create dictionary.
            string keyTypeName   = this.reader.ReadString();
            string valueTypeName = this.reader.ReadString();

            Type keyType   = ReflectionUtils.FindType(keyTypeName);
            Type valueType = ReflectionUtils.FindType(valueTypeName);

            // TODO(np): Deserialize arbitrary dictionary types.
            Type        dictionaryType = typeof(SerializableDictionary <,>).MakeGenericType(keyType, valueType);
            IDictionary dictionary     = (IDictionary)Activator.CreateInstance(dictionaryType);

            // Read data.
            for (int i = 0; i < count; i++)
            {
                object key;
                object value;

                // Read key.
                if (keyType.IsSealed())
                {
                    key = this.Deserialize(keyType);
                }
                else
                {
                    ValueWithType valueWithType = this.DeserializeValueWithType();
                    key = valueWithType.Value;
                }

                // Read value.
                if (valueType.IsSealed())
                {
                    value = this.Deserialize(valueType);
                }
                else
                {
                    ValueWithType valueWithType = this.DeserializeValueWithType();
                    value = valueWithType.Value;
                }

                dictionary.Add(key, value);
            }

            return(dictionary);
        }
        /// <summary>
        ///   Serializes an object to a dictionary.
        /// </summary>
        /// <param name="context">Serialization parameters, such as custom serializers and version number.</param>
        /// <param name="obj">Object to serialize.</param>
        /// <returns>Dictionary which contains object data.</returns>
        public Dictionary <string, object> Serialize(DictionarySerializationContext context, object obj)
        {
            ValueWithType valueWithType = (ValueWithType)obj;

            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { DataValue, context.Serialize(valueWithType.Value) }
            };

            if (!context.IsRawSerializationPossible(valueWithType.Type))
            {
                data.Add(DataType, valueWithType.TypeFullName);
            }

            return(data);
        }
예제 #11
0
        private void PopulateValues(IDictionary from, System.Collections.Generic.List <KeyValue> to)
        {
            IDictionaryEnumerator ide           = from.GetEnumerator();
            ValueWithType         valueWithType = null;
            KeyValue keyValue = null;

            while (ide.MoveNext())
            {
                keyValue     = new KeyValue();
                keyValue.key = ide.Key.ToString();

                if (ide.Value != null && ide.Value is ArrayList)
                {
                    ArrayList list = (ArrayList)ide.Value;
                    foreach (object value in list)
                    {
                        if (value == null)
                        {
                            throw new ArgumentNullException("NCache query does not support null values. ");
                        }
                        Type type = value.GetType();
                        valueWithType       = new ValueWithType();
                        valueWithType.value = GetValueString(value);
                        valueWithType.type  = value.GetType().FullName;

                        keyValue.value.Add(valueWithType);
                    }
                }
                else
                {
                    if (ide.Value == null)
                    {
                        throw new ArgumentNullException("NCache query does not support null values. ");
                    }
                    Type type = ide.Value.GetType();
                    valueWithType       = new ValueWithType();
                    valueWithType.value = GetValueString(ide.Value);
                    valueWithType.type  = ide.Value.GetType().FullName;

                    keyValue.value.Add(valueWithType);
                }

                to.Add(keyValue);
            }
        }
        /// <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;
        }
        /// <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);
        }
        /// <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);
        }
예제 #15
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);
        }
        public void TestSerializeValueWithType()
        {
            ValueWithType valueWithType = new ValueWithType("test");

            this.AssertSerializable(valueWithType);
        }
예제 #17
0
 private void SerializeValueWithType(ValueWithType valueWithType)
 {
     string typeName = SystemExtensions.RemoveAssemblyInfo(valueWithType.TypeFullName);
     this.writer.Write(typeName);
     this.Serialize(valueWithType.Value, valueWithType.Type);
 }