Exemplo n.º 1
0
        private object DeserializeInternal(int depth)
        {
            if (++depth > this._depthLimit)
            {
                throw new System.ArgumentException(this._s.GetDebugString("RecursionLimit exceeded."));
            }
            char?  nextNonEmptyChar = this._s.GetNextNonEmptyChar();
            char?  c = nextNonEmptyChar;
            object result;

            if (!(c.HasValue ? new int?((int)c.GetValueOrDefault()) : null).HasValue)
            {
                result = null;
            }
            else
            {
                this._s.MovePrev();
                if (this.IsNextElementDateTime())
                {
                    result = this.DeserializeStringIntoDateTime();
                }
                else if (JavaScriptObjectDeserializer.IsNextElementObject(nextNonEmptyChar))
                {
                    System.Collections.Generic.IDictionary <string, object> dictionary = this.DeserializeDictionary(depth);
                    if (dictionary.ContainsKey("__type"))
                    {
                        result = ObjectConverter.ConvertObjectToType(dictionary, null, this._serializer);
                    }
                    else
                    {
                        result = dictionary;
                    }
                }
                else if (JavaScriptObjectDeserializer.IsNextElementArray(nextNonEmptyChar))
                {
                    result = this.DeserializeList(depth);
                }
                else if (JavaScriptObjectDeserializer.IsNextElementString(nextNonEmptyChar))
                {
                    result = this.DeserializeString();
                }
                else
                {
                    result = this.DeserializePrimitiveObject();
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        private static object ConvertDictionaryToObject(System.Collections.Generic.IDictionary <string, object> dictionary, System.Type type, JavaScriptSerializer serializer)
        {
            System.Type type2 = type;
            string      text  = null;
            object      obj   = dictionary;
            object      o;

            if (dictionary.TryGetValue("__type", out o))
            {
                text = (ObjectConverter.ConvertObjectToType(o, typeof(string), serializer) as string);
                if (text != null)
                {
                    if (serializer.TypeResolver != null)
                    {
                        type2 = serializer.TypeResolver.ResolveType(text);
                        if (type2 == null)
                        {
                            throw new System.InvalidOperationException();
                        }
                    }
                    dictionary.Remove("__type");
                }
            }
            JavaScriptConverter javaScriptConverter = null;
            object result;

            if (type2 != null && serializer.ConverterExistsForType(type2, out javaScriptConverter))
            {
                result = javaScriptConverter.Deserialize(dictionary, type2, serializer);
            }
            else
            {
                if (text != null || (type2 != null && ObjectConverter.IsClientInstantiatableType(type2, serializer)))
                {
                    obj = System.Activator.CreateInstance(type2);
                }
                System.Collections.Generic.List <string> list = new System.Collections.Generic.List <string>(dictionary.Keys);
                if (type != null && type.IsGenericType && (typeof(System.Collections.IDictionary).IsAssignableFrom(type) || type.GetGenericTypeDefinition() == ObjectConverter._idictionaryGenericType) && type.GetGenericArguments().Length == 2)
                {
                    System.Type type3 = type.GetGenericArguments()[0];
                    if (type3 != typeof(string) && type3 != typeof(object))
                    {
                        throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Type '{0}' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.", new object[]
                        {
                            type.FullName
                        }));
                    }
                    System.Type type4 = type.GetGenericArguments()[1];
                    System.Collections.IDictionary dictionary2 = null;
                    if (ObjectConverter.IsClientInstantiatableType(type, serializer))
                    {
                        dictionary2 = (System.Collections.IDictionary)System.Activator.CreateInstance(type);
                    }
                    else
                    {
                        System.Type type5 = ObjectConverter._dictionaryGenericType.MakeGenericType(new System.Type[]
                        {
                            type3,
                            type4
                        });
                        dictionary2 = (System.Collections.IDictionary)System.Activator.CreateInstance(type5);
                    }
                    if (dictionary2 != null)
                    {
                        foreach (string current in list)
                        {
                            dictionary2[current] = ObjectConverter.ConvertObjectToType(dictionary[current], type4, serializer);
                        }
                        result = dictionary2;
                        return(result);
                    }
                }
                if (type != null && !type.IsAssignableFrom(obj.GetType()))
                {
                    System.Reflection.ConstructorInfo constructor = type.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, ObjectConverter.s_emptyTypeArray, null);
                    if (constructor == null)
                    {
                        throw new System.MissingMethodException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "No parameterless constructor defined for type of '{0}'.", new object[]
                        {
                            type.FullName
                        }));
                    }
                    throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Cannot deserialize object graph into type of '{0}'.", new object[]
                    {
                        type.FullName
                    }));
                }
                else
                {
                    foreach (string current in list)
                    {
                        object propertyValue = dictionary[current];
                        ObjectConverter.AssignToPropertyOrField(propertyValue, obj, current, serializer);
                    }
                    result = obj;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 public T ConvertToType <T>(object obj)
 {
     return((T)((object)ObjectConverter.ConvertObjectToType(obj, typeof(T), this)));
 }
Exemplo n.º 4
0
 private static System.Collections.IList ConvertListToObject(System.Collections.IList list, System.Type type, JavaScriptSerializer serializer)
 {
     System.Type type2;
     System.Collections.IList result;
     if (!(type == null) && !(type == typeof(object)) && !type.IsArray && !(type == typeof(System.Collections.ArrayList)) && !(type == typeof(System.Collections.IEnumerable)) && !(type == typeof(System.Collections.IList)) && !(type == typeof(System.Collections.ICollection)))
     {
         if (type.IsGenericType && type.GetGenericArguments().Length == 1)
         {
             type2 = type.GetGenericArguments()[0];
             System.Type type3 = ObjectConverter._enumerableGenericType.MakeGenericType(new System.Type[]
             {
                 type2
             });
             if (type3.IsAssignableFrom(type))
             {
                 System.Type type4 = ObjectConverter._listGenericType.MakeGenericType(new System.Type[]
                 {
                     type2
                 });
                 System.Collections.IList list2;
                 if (ObjectConverter.IsClientInstantiatableType(type, serializer) && typeof(System.Collections.IList).IsAssignableFrom(type))
                 {
                     list2 = (System.Collections.IList)System.Activator.CreateInstance(type);
                 }
                 else
                 {
                     if (type4.IsAssignableFrom(type))
                     {
                         throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Cannot create instance of {0}.", new object[]
                         {
                             type.FullName
                         }));
                     }
                     list2 = (System.Collections.IList)System.Activator.CreateInstance(type4);
                 }
                 ObjectConverter.AddItemToList(list, list2, type2, serializer);
                 result = list2;
                 return(result);
             }
         }
         else if (ObjectConverter.IsClientInstantiatableType(type, serializer) && typeof(System.Collections.IList).IsAssignableFrom(type))
         {
             System.Collections.IList list2 = (System.Collections.IList)System.Activator.CreateInstance(type);
             ObjectConverter.AddItemToList(list, list2, null, serializer);
             result = list2;
             return(result);
         }
         throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Type '{0}' is not supported for deserialization of an array.", new object[]
         {
             type.FullName
         }));
     }
     type2 = typeof(object);
     if (type != null && type != typeof(object))
     {
         type2 = type.GetElementType();
     }
     System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
     ObjectConverter.AddItemToList(list, arrayList, type2, serializer);
     if (type == typeof(System.Collections.ArrayList) || type == typeof(System.Collections.IEnumerable) || type == typeof(System.Collections.IList) || type == typeof(System.Collections.ICollection))
     {
         result = arrayList;
     }
     else
     {
         result = arrayList.ToArray(type2);
     }
     return(result);
 }