internal object ConvertToType(Type type, object obj) { var primitiveConverter = GetPrimitiveConverter(type); if (primitiveConverter != null) { obj = primitiveConverter.Deserialize(obj, type, this); } if (obj == null) { return(null); } if (obj is IDictionary <string, object> ) { if (type == null) { obj = EvaluateDictionary((IDictionary <string, object>)obj); } else { JavaScriptConverter converter = GetConverter(type); if (converter != null) { return(converter.Deserialize( EvaluateDictionary((IDictionary <string, object>)obj), type, this)); } } return(ConvertToObject((IDictionary <string, object>)obj, type)); } if (obj is ArrayList) { return(ConvertToList((ArrayList)obj, type)); } if (type == null) { return(obj); } Type sourceType = obj.GetType(); if (type.IsAssignableFrom(sourceType)) { return(obj); } if (type.IsEnum) { if (obj is string) { return(Enum.Parse(type, (string)obj, true)); } else { return(Enum.ToObject(type, obj)); } } TypeConverter c = TypeDescriptor.GetConverter(type); if (c.CanConvertFrom(sourceType)) { if (obj is string) { return(c.ConvertFromInvariantString((string)obj)); } return(c.ConvertFrom(obj)); } if (type == typeof(DateTimeOffset) && obj is string) { return(DateTimeOffset.Parse((string)obj)); } if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable <>))) { /* * Take care of the special case whereas in JSON an empty string ("") really means * an empty value * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836) */ string s = obj as String; if (s != null) { if (s == string.Empty) { return(null); } } else //It is not string at all, convert to Nullable<> type, from int to uint for example { return(Convert.ChangeType(obj, type.GetGenericArguments()[0])); } } return(Convert.ChangeType(obj, type)); }
internal object ConvertToType(Type type, object obj) { if (obj == null) { return(null); } if (obj is IDictionary <string, object> ) { if (type == null) { obj = EvaluateDictionary((IDictionary <string, object>)obj); } else { JavaScriptConverter converter = GetConverter(type); if (converter != null) { return(converter.Deserialize( EvaluateDictionary((IDictionary <string, object>)obj), type, this)); } } return(ConvertToObject((IDictionary <string, object>)obj, type)); } if (obj is ArrayList) { return(ConvertToList((ArrayList)obj, type)); } if (type == null) { return(obj); } Type sourceType = obj.GetType(); if (type.IsAssignableFrom(sourceType)) { return(obj); } if (type.IsEnum) { if (obj is string) { return(Enum.Parse(type, (string)obj, true)); } else { return(Enum.ToObject(type, obj)); } } TypeConverter c = TypeDescriptor.GetConverter(type); if (c.CanConvertFrom(sourceType)) { if (obj is string) { return(c.ConvertFromInvariantString((string)obj)); } return(c.ConvertFrom(obj)); } /* * Take care of the special case whereas in JSON an empty string ("") really means * an empty value * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836) */ if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable <>))) { string s = obj as String; if (String.IsNullOrEmpty(s)) { return(null); } } return(Convert.ChangeType(obj, type)); }