public virtual object DeserializeObject(object value, Type type) { if (type == null) { throw new ArgumentNullException("type"); } string text = value as string; object result; if (type == typeof(Guid) && string.IsNullOrEmpty(text)) { result = default(Guid); } else if (value == null) { result = null; } else { object obj = null; if (text != null) { if (text.Length != 0) { if (type.IsEnum) { return(Enum.Parse(type, text)); } if (type == typeof(DateTime) || (ReflectionsUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTime))) { result = DateTime.ParseExact(text, PocoJsonSerializerStrategy.Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); return(result); } if (type == typeof(DateTimeOffset) || (ReflectionsUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset))) { result = DateTimeOffset.ParseExact(text, PocoJsonSerializerStrategy.Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); return(result); } if (type == typeof(Guid) || (ReflectionsUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid))) { result = new Guid(text); return(result); } if (type == typeof(Uri)) { result = new Uri(text); return(result); } result = text; return(result); } else { if (type == typeof(Guid)) { obj = default(Guid); } else if (ReflectionsUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)) { obj = null; } else { obj = text; } if (!ReflectionsUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)) { result = text; return(result); } } } else if (value is bool) { result = value; return(result); } bool flag = value is long; bool flag2 = value is double; if ((flag && type == typeof(long)) || (flag2 && type == typeof(double))) { result = value; } else if ((flag2 && type != typeof(double)) || (flag && type != typeof(long))) { obj = ((!typeof(IConvertible).IsAssignableFrom(type)) ? value : Convert.ChangeType(value, type, CultureInfo.InvariantCulture)); if (ReflectionsUtils.IsNullableType(type)) { result = ReflectionsUtils.ToNullableType(obj, type); } else { result = obj; } } else { IDictionary <string, object> dictionary = value as IDictionary <string, object>; if (dictionary != null) { IDictionary <string, object> dictionary2 = dictionary; if (ReflectionsUtils.IsTypeDictionary(type)) { Type[] genericTypeArguments = ReflectionsUtils.GetGenericTypeArguments(type); Type type2 = genericTypeArguments[0]; Type type3 = genericTypeArguments[1]; Type key = typeof(Dictionary <, >).MakeGenericType(new Type[] { type2, type3 }); IDictionary dictionary3 = (IDictionary)this.ConstructorCache[key](null); foreach (KeyValuePair <string, object> current in dictionary2) { dictionary3.Add(current.Key, this.DeserializeObject(current.Value, type3)); } obj = dictionary3; } else if (type == typeof(object)) { obj = value; } else { obj = this.ConstructorCache[type](null); foreach (KeyValuePair <string, KeyValuePair <Type, ReflectionsUtils.SetDelegate> > current2 in this.SetCache[type]) { object value2; if (dictionary2.TryGetValue(current2.Key, out value2)) { value2 = this.DeserializeObject(value2, current2.Value.Key); current2.Value.Value(obj, value2); } } } } else { IList <object> list = value as IList <object>; if (list != null) { IList <object> list2 = list; IList list3 = null; if (type.IsArray) { list3 = (IList)this.ConstructorCache[type](new object[] { list2.Count }); int num = 0; foreach (object current3 in list2) { list3[num++] = this.DeserializeObject(current3, type.GetElementType()); } } else if (ReflectionsUtils.IsTypeGenericeCollectionInterface(type) || ReflectionsUtils.IsAssignableFrom(typeof(IList), type)) { Type type4 = ReflectionsUtils.GetGenericTypeArguments(type)[0]; Type key2 = typeof(List <>).MakeGenericType(new Type[] { type4 }); list3 = (IList)this.ConstructorCache[key2](new object[] { list2.Count }); foreach (object current4 in list2) { list3.Add(this.DeserializeObject(current4, type4)); } } obj = list3; } } result = obj; } } return(result); }
public static object DeserializeObject(string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy) { object obj = SimpleJsonTool.DeserializeObject(json); return((type != null && (obj == null || !ReflectionsUtils.IsAssignableFrom(obj.GetType(), type))) ? (jsonSerializerStrategy ?? SimpleJsonTool.CurrentJsonSerializerStrategy).DeserializeObject(obj, type) : obj); }