private static void AssignToPropertyOrField(object propertyValue, object o, string memberName, JavaScriptSerializer serializer) { System.Collections.IDictionary dictionary = o as System.Collections.IDictionary; if (dictionary != null) { propertyValue = ObjectConverter.ConvertObjectToType(propertyValue, null, serializer); dictionary[memberName] = propertyValue; } else { System.Type type = o.GetType(); System.Reflection.PropertyInfo property = type.GetProperty(memberName, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (property != null) { System.Reflection.MethodInfo setMethod = property.GetSetMethod(); if (setMethod != null) { propertyValue = ObjectConverter.ConvertObjectToType(propertyValue, property.PropertyType, serializer); setMethod.Invoke(o, new object[] { propertyValue }); return; } } System.Reflection.FieldInfo field = type.GetField(memberName, System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (field != null) { propertyValue = ObjectConverter.ConvertObjectToType(propertyValue, field.FieldType, serializer); field.SetValue(o, propertyValue); } } }
private static void AddItemToList(System.Collections.IList oldList, System.Collections.IList newList, System.Type elementType, JavaScriptSerializer serializer) { foreach (object current in oldList) { newList.Add(ObjectConverter.ConvertObjectToType(current, elementType, serializer)); } }
internal static object Deserialize(JavaScriptSerializer serializer, string input, System.Type type, int depthLimit) { if (input == null) { throw new System.ArgumentNullException("input"); } if (input.Length > serializer.MaxJsonLength) { throw new System.ArgumentException("结果记录数太大,请调整查询条件后重试", "input"); } object o = JavaScriptObjectDeserializer.BasicDeserialize(input, depthLimit, serializer); return(ObjectConverter.ConvertObjectToType(o, type, serializer)); }
private object DeserializeInternal(int depth) { if (++depth > this._depthLimit) { throw new System.ArgumentException(this._s.GetDebugString("RecursionLimit exceeded.")); } char? nextNonEmptyChar = this._s.GetNextNonEmptyChar(); char? c = nextNonEmptyChar; object result; if (!(c.HasValue ? new int?((int)c.GetValueOrDefault()) : null).HasValue) { result = null; } else { this._s.MovePrev(); if (this.IsNextElementDateTime()) { result = this.DeserializeStringIntoDateTime(); } else if (JavaScriptObjectDeserializer.IsNextElementObject(nextNonEmptyChar)) { System.Collections.Generic.IDictionary <string, object> dictionary = this.DeserializeDictionary(depth); if (dictionary.ContainsKey("__type")) { result = ObjectConverter.ConvertObjectToType(dictionary, null, this._serializer); } else { result = dictionary; } } else if (JavaScriptObjectDeserializer.IsNextElementArray(nextNonEmptyChar)) { result = this.DeserializeList(depth); } else if (JavaScriptObjectDeserializer.IsNextElementString(nextNonEmptyChar)) { result = this.DeserializeString(); } else { result = this.DeserializePrimitiveObject(); } } return(result); }
public T ConvertToType <T>(object obj) { return((T)((object)ObjectConverter.ConvertObjectToType(obj, typeof(T), this))); }
private static object ConvertDictionaryToObject(System.Collections.Generic.IDictionary <string, object> dictionary, System.Type type, JavaScriptSerializer serializer) { System.Type type2 = type; string text = null; object obj = dictionary; object o; if (dictionary.TryGetValue("__type", out o)) { text = (ObjectConverter.ConvertObjectToType(o, typeof(string), serializer) as string); if (text != null) { if (serializer.TypeResolver != null) { type2 = serializer.TypeResolver.ResolveType(text); if (type2 == null) { throw new System.InvalidOperationException(); } } dictionary.Remove("__type"); } } JavaScriptConverter javaScriptConverter = null; object result; if (type2 != null && serializer.ConverterExistsForType(type2, out javaScriptConverter)) { result = javaScriptConverter.Deserialize(dictionary, type2, serializer); } else { if (text != null || (type2 != null && ObjectConverter.IsClientInstantiatableType(type2, serializer))) { obj = System.Activator.CreateInstance(type2); } System.Collections.Generic.List <string> list = new System.Collections.Generic.List <string>(dictionary.Keys); if (type != null && type.IsGenericType && (typeof(System.Collections.IDictionary).IsAssignableFrom(type) || type.GetGenericTypeDefinition() == ObjectConverter._idictionaryGenericType) && type.GetGenericArguments().Length == 2) { System.Type type3 = type.GetGenericArguments()[0]; if (type3 != typeof(string) && type3 != typeof(object)) { throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Type '{0}' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.", new object[] { type.FullName })); } System.Type type4 = type.GetGenericArguments()[1]; System.Collections.IDictionary dictionary2 = null; if (ObjectConverter.IsClientInstantiatableType(type, serializer)) { dictionary2 = (System.Collections.IDictionary)System.Activator.CreateInstance(type); } else { System.Type type5 = ObjectConverter._dictionaryGenericType.MakeGenericType(new System.Type[] { type3, type4 }); dictionary2 = (System.Collections.IDictionary)System.Activator.CreateInstance(type5); } if (dictionary2 != null) { foreach (string current in list) { dictionary2[current] = ObjectConverter.ConvertObjectToType(dictionary[current], type4, serializer); } result = dictionary2; return(result); } } if (type != null && !type.IsAssignableFrom(obj.GetType())) { System.Reflection.ConstructorInfo constructor = type.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, ObjectConverter.s_emptyTypeArray, null); if (constructor == null) { throw new System.MissingMethodException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "No parameterless constructor defined for type of '{0}'.", new object[] { type.FullName })); } throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Cannot deserialize object graph into type of '{0}'.", new object[] { type.FullName })); } else { foreach (string current in list) { object propertyValue = dictionary[current]; ObjectConverter.AssignToPropertyOrField(propertyValue, obj, current, serializer); } result = obj; } } return(result); }