Exemple #1
0
 internal bool ConverterExistsForType(Type t, out JavaScriptConverter converter)
 {
     converter = this.GetConverter(t);
     return(converter != null);
 }
Exemple #2
0
        private static bool ConvertDictionaryToObject(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject)
        {
            object obj2;
            Type   t  = type;
            string id = null;
            object o  = dictionary;

            if (dictionary.TryGetValue("__type", out obj2))
            {
                if (!ConvertObjectToTypeMain(obj2, typeof(string), serializer, throwOnError, out obj2))
                {
                    convertedObject = false;
                    return(false);
                }
                id = (string)obj2;
                if (id != null)
                {
                    if (serializer.TypeResolver != null)
                    {
                        t = serializer.TypeResolver.ResolveType(id);
                        if (t == null)
                        {
                            if (throwOnError)
                            {
                                throw new InvalidOperationException();
                            }
                            convertedObject = null;
                            return(false);
                        }
                    }
                    dictionary.Remove("__type");
                }
            }
            JavaScriptConverter converter = null;

            if ((t != null) && serializer.ConverterExistsForType(t, out converter))
            {
                try
                {
                    convertedObject = converter.Deserialize(dictionary, t, serializer);
                    return(true);
                }
                catch
                {
                    if (throwOnError)
                    {
                        throw;
                    }
                    convertedObject = null;
                    return(false);
                }
            }
            if ((id != null) || IsClientInstantiatableType(t, serializer))
            {
                o = Activator.CreateInstance(t);
            }
            List <string> list = new List <string>(dictionary.Keys);

            if (IsGenericDictionary(type))
            {
                Type type3 = type.GetGenericArguments()[0];
                if ((type3 != typeof(string)) && (type3 != typeof(object)))
                {
                    if (throwOnError)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, JSON_DictionaryTypeNotSupported, new object[] { type.FullName }));
                    }
                    convertedObject = null;
                    return(false);
                }
                Type        type4       = type.GetGenericArguments()[1];
                IDictionary dictionary2 = null;
                if (IsClientInstantiatableType(type, serializer))
                {
                    dictionary2 = (IDictionary)Activator.CreateInstance(type);
                }
                else
                {
                    dictionary2 = (IDictionary)Activator.CreateInstance(_dictionaryGenericType.MakeGenericType(new Type[] { type3, type4 }));
                }
                if (dictionary2 != null)
                {
                    foreach (string str2 in list)
                    {
                        object obj4;
                        if (!ConvertObjectToTypeMain(dictionary[str2], type4, serializer, throwOnError, out obj4))
                        {
                            convertedObject = null;
                            return(false);
                        }
                        dictionary2[str2] = obj4;
                    }
                    convertedObject = dictionary2;
                    return(true);
                }
            }
            if ((type != null) && !type.IsAssignableFrom(o.GetType()))
            {
                if (!throwOnError)
                {
                    convertedObject = null;
                    return(false);
                }
                if (type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null) == null)
                {
                    throw new MissingMethodException(string.Format(CultureInfo.InvariantCulture, JSON_NoConstructor, new object[] { type.FullName }));
                }
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, JSON_DeserializerTypeMismatch, new object[] { type.FullName }));
            }
            foreach (string str3 in list)
            {
                object propertyValue = dictionary[str3];
                if (!AssignToPropertyOrField(propertyValue, o, str3, serializer, throwOnError))
                {
                    convertedObject = null;
                    return(false);
                }
            }
            convertedObject = o;
            return(true);
        }