/// <summary> /// De-serialize an array into a list of the correct type/ /// </summary> /// <param name="value">The json list to retrieve the values from.</param> /// <param name="type">The type of the returned object.</param> /// <param name="obj"></param> public virtual void DeserializeArray(IList <object> value, Type type, out object obj) { obj = null; JsonArray list = value as JsonArray; IList resultList = null; if (type.IsArray) { resultList = (IList)Activator.CreateInstance(type, new object[] { list.Count }); for (int i = 0; i < list.Count; i++) { resultList[i] = this.DeserializeObject(resultList[i], type.GetElementType()); } } else { if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || typeof(IList).IsAssignableFrom(type)) { Type listType = type.GetGenericArguments()[0]; resultList = (IList)CacheResolver.GetNewInstance(type); for (int i = 0; i < list.Count; i++) { object listValue = list[i]; if (listValue != null && listValue is JsonObject) { JsonObject dataObj = (JsonObject)listValue; if (dataObj.ContainsKey("@type")) { Type objType = Type.GetType((string)dataObj["@type"]); resultList.Add(this.DeserializeObject(listValue, objType)); continue; } } resultList.Add(this.DeserializeObject(listValue, listType)); } } obj = resultList; } }
/// <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 } } } }