Esempio n. 1
0
        /// <summary>
        /// De-serialize a dictionary into a dictionary of the correct type or into an object
        /// </summary>
        /// <param name="dict">The json dictionary to retrieve the values from.</param>
        /// <param name="type">The type of the returned object.</param>
        /// <param name="obj">The object that is returned.</param>
        public virtual void DeserializeDictionary(IDictionary <string, object> dict, Type type, out object obj)
        {
            obj = null;
            if (ReflectionUtils.IsTypeDictionary(type))
            {
                Type        keyType     = type.GetGenericArguments()[0];
                Type        valueType   = type.GetGenericArguments()[1];
                IDictionary dictionary2 = (IDictionary)CacheResolver.GetNewInstance(type);
                foreach (KeyValuePair <string, object> current in dict)
                {
                    Type   objType = valueType;
                    object data    = current.Value;

                    if (current.Value != null && current.Value.GetType() == typeof(JsonObject))
                    {
                        JsonObject dataObj = (JsonObject)current.Value;
                        if (dataObj.ContainsKey("@type"))
                        {
                            objType = Type.GetType((string)dataObj["@type"]);
                        }
                    }

                    dictionary2.Add(current.Key, this.DeserializeObject(data, objType));
                }
                obj = dictionary2;
            }
            else if (TryDeserializeCustomType(type, dict, out obj))
            {
                // The function should handle any conversions that are required.
            }
            else
            {
                obj = CacheResolver.GetNewInstance(type);
                Dictionary <string, CacheResolver.MemberMap> memberDict = _cacheResolver.LoadMaps(type);
                if (memberDict == null)
                {
                    obj = dict;
                }
                else
                {
                    foreach (KeyValuePair <string, CacheResolver.MemberMap> objMember in memberDict)
                    {
                        CacheResolver.MemberMap memberVal = objMember.Value;
                        if (memberVal.Setter != null)
                        {
                            string key = objMember.Key;
                            if (dict.ContainsKey(key))
                            {
                                Type   objType = memberVal.Type;
                                object data    = dict[key];

                                if (data != null && data is JsonObject)
                                {
                                    JsonObject dataObj = (JsonObject)data;
                                    if (dataObj.ContainsKey("@type"))
                                    {
                                        objType = Type.GetType((string)dataObj["@type"]);
                                    }
                                }

                                object memberResult = this.DeserializeObject(data, objType);
                                memberVal.Setter(obj, memberResult);
                            }
                        }
                    }

                    if (typeof(IJsonSerializable).IsAssignableFrom(type))
                    {
                        // TODO:
                        // obj
                    }
                }
            }
        }
Esempio n. 2
0
        public virtual object deserializeObject(object value, Type type)
        {
            object result = null;

            if (value is string)
            {
                string text = value as string;
                result = ((string.IsNullOrEmpty(text) || (type != typeof(DateTime) && (!ReflectionUtils.isNullableType(type) || Nullable.GetUnderlyingType(type) != typeof(DateTime)))) ? text : ((object)DateTime.ParseExact(text, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal)));
            }
            else if (value is bool)
            {
                result = value;
            }
            else if (value == null)
            {
                result = null;
            }
            else if ((value is long && type == typeof(long)) || (value is double && type == typeof(double)))
            {
                result = value;
            }
            else
            {
                if ((!(value is double) || type == typeof(double)) && (!(value is long) || type == typeof(long)))
                {
                    if (value is IDictionary <string, object> )
                    {
                        IDictionary <string, object> dictionary = (IDictionary <string, object>)value;
                        if (ReflectionUtils.isTypeDictionary(type))
                        {
                            Type        type2       = type.GetGenericArguments()[0];
                            Type        type3       = type.GetGenericArguments()[1];
                            Type        type4       = typeof(Dictionary <, >).MakeGenericType(type2, type3);
                            IDictionary dictionary2 = (IDictionary)CacheResolver.getNewInstance(type4);
                            foreach (KeyValuePair <string, object> item in dictionary)
                            {
                                dictionary2.Add(item.Key, deserializeObject(item.Value, type3));
                            }
                            result = dictionary2;
                        }
                        else
                        {
                            result = CacheResolver.getNewInstance(type);
                            SafeDictionary <string, CacheResolver.MemberMap> safeDictionary = cacheResolver.loadMaps(type);
                            if (safeDictionary != null)
                            {
                                {
                                    foreach (KeyValuePair <string, CacheResolver.MemberMap> item2 in safeDictionary)
                                    {
                                        CacheResolver.MemberMap value2 = item2.Value;
                                        if (value2.Setter != null)
                                        {
                                            string key = item2.Key;
                                            if (dictionary.ContainsKey(key))
                                            {
                                                object value3 = deserializeObject(dictionary[key], value2.Type);
                                                value2.Setter(result, value3);
                                            }
                                        }
                                    }
                                    return(result);
                                }
                            }
                            result = value;
                        }
                    }
                    else if (value is IList <object> )
                    {
                        IList <object> list  = (IList <object>)value;
                        IList          list2 = null;
                        if (type.IsArray)
                        {
                            list2 = (IList)Activator.CreateInstance(type, list.Count);
                            int num = 0;
                            foreach (object item3 in list)
                            {
                                list2[num++] = deserializeObject(item3, type.GetElementType());
                            }
                        }
                        else if (ReflectionUtils.isTypeGenericeCollectionInterface(type) || typeof(IList).IsAssignableFrom(type))
                        {
                            Type type5 = type.GetGenericArguments()[0];
                            Type type6 = typeof(List <>).MakeGenericType(type5);
                            list2 = (IList)CacheResolver.getNewInstance(type6);
                            foreach (object item4 in list)
                            {
                                list2.Add(deserializeObject(item4, type5));
                            }
                        }
                        result = list2;
                    }
                    return(result);
                }
                result = ((value is long && type == typeof(DateTime)) ? ((object)new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)value)) : ((!type.IsEnum) ? ((!typeof(IConvertible).IsAssignableFrom(type)) ? value : Convert.ChangeType(value, type, CultureInfo.InvariantCulture)) : Enum.ToObject(type, value)));
            }
            if (ReflectionUtils.isNullableType(type))
            {
                return(ReflectionUtils.toNullableType(result, type));
            }
            return(result);
        }