private static object DeserializeArray(Type type, List <object> list) { if (list == null || list.Count == 0) { return(null); } if (type.IsArray) { Type elementType = type.GetElementType(); Array v = Array.CreateInstance(elementType, list.Count); for (int i = 0; i < list.Count; i++) { object child = list[i]; object item; if (child is IDictionary) { item = DeserializeObject(elementType, child as Dictionary <string, object>); } else if (child is IList) { item = DeserializeArray(elementType, child as List <object>); } else { item = DeserializeValue(elementType, child); } v.SetValue(item, i); } return(v); } if (OnlineMapsReflectionHelper.IsGenericType(type)) { Type listType = OnlineMapsReflectionHelper.GetGenericArguments(type)[0]; object v = Activator.CreateInstance(type); for (int i = 0; i < list.Count; i++) { object child = list[i]; object item; if (child is IDictionary) { item = DeserializeObject(listType, child as Dictionary <string, object>); } else if (child is IList) { item = DeserializeArray(listType, child as List <object>); } else { item = DeserializeValue(listType, child); } try { MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add"); if (methodInfo != null) { methodInfo.Invoke(v, new[] { item }); } } catch { } } return(v); } return(null); }
public override object Deserialize(Type type) { if (_count == 0) { return(null); } if (type.IsArray) { Type elementType = type.GetElementType(); Array v = Array.CreateInstance(elementType, _count); if (array[0] is OnlineMapsJSONObject) { IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(elementType, BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = (child as OnlineMapsJSONObject).Deserialize(elementType, members); v.SetValue(item, i); } } else { for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = child.Deserialize(elementType); v.SetValue(item, i); } } return(v); } if (OnlineMapsReflectionHelper.IsGenericType(type)) { Type listType = OnlineMapsReflectionHelper.GetGenericArguments(type)[0]; object v = Activator.CreateInstance(type); if (array[0] is OnlineMapsJSONObject) { IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(listType, BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = (child as OnlineMapsJSONObject).Deserialize(listType, members); try { MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add"); if (methodInfo != null) { methodInfo.Invoke(v, new[] { item }); } } catch { } } } else { for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = child.Deserialize(listType); try { MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add"); if (methodInfo != null) { methodInfo.Invoke(v, new[] { item }); } } catch { } } } return(v); } return(null); }
public override object Value(Type t) { if (_type == ValueType.NULL || _value == null) { if (OnlineMapsReflectionHelper.IsValueType(t)) { return(Activator.CreateInstance(t)); } return(null); } if (t == typeof(string)) { return(Convert.ChangeType(_value, t)); } if (_type == ValueType.BOOLEAN) { if (t == typeof(bool)) { return(Convert.ChangeType(_value, t)); } } else if (_type == ValueType.DOUBLE) { if (t == typeof(double)) { return(Convert.ChangeType(_value, t, OnlineMapsUtils.numberFormat)); } if (t == typeof(float)) { return(Convert.ChangeType((double)_value, t, OnlineMapsUtils.numberFormat)); } } else if (_type == ValueType.LONG) { if (t == typeof(long)) { return(Convert.ChangeType(_value, t)); } #if UNITY_EDITOR if (t.IsSubclassOf(typeof(UnityEngine.Object))) { return(UnityEditor.EditorUtility.InstanceIDToObject((int)(long)_value)); } #endif try { return(Convert.ChangeType((long)_value, t)); } catch (Exception e) { Debug.Log(e.Message + "\n" + e.StackTrace); return(null); } } else if (_type == ValueType.STRING) { MethodInfo method = OnlineMapsReflectionHelper.GetMethod(t, "Parse", new[] { typeof(string), typeof(IFormatProvider) }); if (method != null) { return(method.Invoke(null, new object[] { value, OnlineMapsUtils.numberFormat })); } method = OnlineMapsReflectionHelper.GetMethod(t, "Parse", new[] { typeof(string) }); return(method.Invoke(null, new[] { value })); } StringBuilder builder = new StringBuilder(); ToJSON(builder); throw new InvalidCastException(t.FullName + "\n" + builder); }
public override object Value(Type t) { if (_type == ValueType.NULL || _value == null) { if (OnlineMapsReflectionHelper.IsValueType(t)) { return(Activator.CreateInstance(t)); } return(null); } if (t == typeof(string)) { return(Convert.ChangeType(_value, t)); } if (_type == ValueType.BOOLEAN) { if (t == typeof(bool)) { return(Convert.ChangeType(_value, t)); } } else if (_type == ValueType.DOUBLE) { if (t == typeof(double)) { return(Convert.ChangeType(_value, t)); } if (t == typeof(float)) { return(Convert.ChangeType((double)_value, t)); } } else if (_type == ValueType.LONG) { if (t == typeof(long)) { return(Convert.ChangeType(_value, t)); } if (t == typeof(int)) { return(Convert.ChangeType((long)_value, t)); } if (t == typeof(float)) { return(Convert.ChangeType((long)_value, t)); } if (t == typeof(double)) { return(Convert.ChangeType((long)_value, t)); } } else if (_type == ValueType.STRING) { MethodInfo method = OnlineMapsReflectionHelper.GetMethod(t, "Parse", new[] { typeof(string) }); if (method != null) { return(method.Invoke(null, new[] { _value })); } return(null); } StringBuilder builder = new StringBuilder(); ToJSON(builder); throw new InvalidCastException(t.FullName + "\n" + builder); }