Exemplo n.º 1
0
        private static object CreateClassFromDic(Dictionary <string, object> dic, Type type, object parent)
        {
            var typename = type.FullName;
            var result   = parent ?? Reflection.Instance.FastCreateInstance(type);

            var props = Reflection.Instance.Getproperties(type, typename);

            foreach (var kv in dic)
            {
                var key     = kv.Key;
                var value   = kv.Value;
                var keyName = key.ToLower();

                var containsNameInProps = props.ContainsKey(keyName);
                if (!containsNameInProps)
                {
                    continue;
                }
                var    propInfo = props[keyName];
                object oset     = null;

                switch (propInfo.Type)
                {
                case MyPropInfoType.Int: oset = (int)(long)value; break;

                case MyPropInfoType.Float: oset = (float)Deserializer.TryGetDoubleValue(value); break;

                case MyPropInfoType.String: oset = (string)value; break;

                case MyPropInfoType.Bool: oset = (bool)value; break;

                case MyPropInfoType.Char: oset = ((string)value)[0]; break;

                case MyPropInfoType.Byte: oset = (byte)(long)value; break;

                case MyPropInfoType.Decimal: oset = (decimal)Deserializer.TryGetDoubleValue(value); break;

                case MyPropInfoType.Double: oset = Deserializer.TryGetDoubleValue(value); break;

                case MyPropInfoType.Short: oset = (short)(long)value; break;

                case MyPropInfoType.Long: oset = (long)value; break;

                case MyPropInfoType.DateTime: oset = Deserializer.CreateDateTime((string)value); break;

                case MyPropInfoType.Enum: oset = Deserializer.CreateEnum(propInfo.Pt, value); break;

                case MyPropInfoType.Guid: oset = Deserializer.CreateGuid((string)value); break;

                case MyPropInfoType.List: oset = CreateList((List <object>)value, propInfo.Pt); break;

                case MyPropInfoType.Array: oset = Deserializer.CreateArray((List <object>)value, propInfo.Bt, null); break;

                case MyPropInfoType.ByteArray: oset = Convert.FromBase64String((string)value); break;

                case MyPropInfoType.Hashtable:                         // same case as Dictionary
                case MyPropInfoType.Dictionary: oset = Deserializer.CreateDictionary((List <object>)value, propInfo.Pt, propInfo.GenericTypes, null); break;

                case MyPropInfoType.StringKeyDictionary: oset = Deserializer.CreateStringKeyDictionary((Dictionary <string, object>)value, propInfo.Pt, propInfo.GenericTypes, null); break;

                case MyPropInfoType.NameValue: oset = Deserializer.CreateNv((Dictionary <string, object>)value); break;

                case MyPropInfoType.StringDictionary: oset = Deserializer.CreateSd((Dictionary <string, object>)value); break;

                    #region Unity_Build-in_Classes
                case MyPropInfoType.Vector2:
                    oset = VectorFactory.CreateVector2((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Vector2Int:
                    oset = VectorFactory.CreateVector2Int((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Vector3:
                    oset = VectorFactory.CreateVector3((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Vector3Int:
                    oset = VectorFactory.CreateVector3Int((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Vector4:
                    oset = VectorFactory.CreateVector4((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Color:
                    oset = UnityClassesFactoryMain.CreateColor((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Color32:
                    oset = UnityClassesFactoryMain.CreateColor32((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Rect:
                    oset = UnityClassesFactoryMain.CreateRect((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.RectInt:
                    oset = UnityClassesFactoryMain.CreateRectInt((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Bounds:
                    oset = UnityClassesFactoryMain.CreateBounds((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.BoundsInt:
                    oset = UnityClassesFactoryMain.CreateBoundsInt((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Ray:
                    oset = UnityClassesFactoryMain.CreateRay((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Ray2D:
                    oset = UnityClassesFactoryMain.CreateRay2D((Dictionary <string, object>)value);
                    break;

                case MyPropInfoType.Quaternion:
                    oset = UnityClassesFactoryMain.CreateQuaternion((Dictionary <string, object>)value);
                    break;

                    #endregion
                case MyPropInfoType.Custom: oset = Reflection.Instance.CreateCustom((string)value, propInfo.Pt); break;

                case MyPropInfoType.Unknown:
                    UnityEngine.Debug.LogError("Your class is unsupported: " + propInfo.Pt + ". Pls register with JSONObject.RegisterCustomType().");
                    break;

                default:
                {
                    if (propInfo.IsGenericType && propInfo.IsValueType == false && value is List <object> )
                    {
                        oset = CreateList((List <object>)value, propInfo.Pt);
                    }

                    else if ((propInfo.IsClass || propInfo.IsStruct || propInfo.IsInterface) && value is Dictionary <string, object> )
                    {
                        oset = CreateClassFromDic((Dictionary <string, object>)value, propInfo.Pt, propInfo.Getter(result));
                    }

                    else if (value is List <object> )
                    {
                        oset = Deserializer.CreateArray((List <object>)value, typeof(object), null);
                    }

                    else if (propInfo.IsValueType)
                    {
                        oset = Deserializer.ChangeType(value, propInfo.ChangeType);
                    }

                    else
                    {
                        oset = value;
                    }
                }
                break;
                }

                result = propInfo.Setter(result, oset);
            }

            return(result);
        }
Exemplo n.º 2
0
        public T TryGetValue <T>()
        {
            var type = typeof(T);

            if (_nodeObject.ElementType == JsonElementType.Long)
            {
                if (type == typeof(int))
                {
                    return((T)(object)(int)(long)_nodeObject.FinishObj);
                }

                if (type == typeof(long))
                {
                    return((T)_nodeObject.FinishObj);
                }

                Debug.Log(type.Name);
            }

            if (_nodeObject.ElementType == JsonElementType.Bool)
            {
                if (type == typeof(bool))
                {
                    return((T)_nodeObject.FinishObj);
                }

                Debug.Log(type.Name);
            }

            if (_nodeObject.ElementType == JsonElementType.Dictionary)
            {
                if (type == typeof(Dictionary <string, object>))
                {
                    return((T)_nodeObject.FinishObj);
                }

                if (type == typeof(StringDictionary))
                {
                    return((T)(object)ClassFactory.GetStringDictionaryValue(_nodeObject.FinishObj));
                }

                if (type == typeof(Dictionary <string, string>))
                {
                    return((T)(object)ClassFactory.GetDictionaryStringStringValue(_nodeObject.FinishObj));
                }

                if (type == typeof(NameValueCollection))
                {
                    return((T)(object)ClassFactory.GetNameValueCollectionValue(_nodeObject.FinishObj));
                }

                #region UNITY_TYPES
                if (type == typeof(Vector2))
                {
                    return((T)(object)VectorFactory.CreateVector2((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Vector2Int))
                {
                    return((T)(object)VectorFactory.CreateVector2Int((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Vector3))
                {
                    return((T)(object)VectorFactory.CreateVector3((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Vector3Int))
                {
                    return((T)(object)VectorFactory.CreateVector3Int((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Vector4))
                {
                    return((T)(object)VectorFactory.CreateVector4((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Color))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateColor((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Color32))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateColor32((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Rect))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateRect((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(RectInt))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateRectInt((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Bounds))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateBounds((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(BoundsInt))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateBoundsInt((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Ray))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateRay((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Ray2D))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateRay2D((Dictionary <string, object>)_nodeObject.FinishObj));
                }

                if (type == typeof(Quaternion))
                {
                    return((T)(object)UnityClassesFactoryMain.CreateQuaternion((Dictionary <string, object>)_nodeObject.FinishObj));
                }
                #endregion

                var result = (T)CustomClassFactory.ParseCustomClass(_nodeObject.FinishObj, type);
                return(result);
            }

            if (_nodeObject.ElementType == JsonElementType.List)
            {
                if (type == typeof(List <object>))
                {
                    return((T)_nodeObject.FinishObj);
                }

                if (type == typeof(Hashtable))
                {
                    return((T)(object)ClassFactory.GetHashtableValue(_nodeObject.FinishObj));
                }

                if (type == typeof(Array))
                {
                    return((T)(object)ClassFactory.GetArrayValue(_nodeObject.FinishObj));
                }

                var result = (T)CustomClassFactory.ParseCustomClass(_nodeObject.FinishObj, type);
                return(result);
            }

            if (_nodeObject.ElementType != JsonElementType.String)
            {
                //Debug.Log(_nodeObject.ElementType);
                return(default(T));
            }

            var str = (string)_nodeObject.FinishObj;


            if (type == typeof(string))
            {
                return((T)(object)str);
            }

            if (type == typeof(DateTime))
            {
                var result = ClassFactory.GetDateTime(str);
                return((T)(object)result);
            }

            if (type == typeof(Guid))
            {
                var result = ClassFactory.GetGuidValue(str);
                return((T)(object)result);
            }

            if (type.IsEnum)
            {
                if (type == typeof(Enum))
                {
                    Debug.LogError("Pls get concrete enum type, not abstract enum");
                    return(default(T));
                }

                var result = ClassFactory.GetEnumValue <T>(str);
                return(result);
            }

            if (type == typeof(byte[]))
            {
                var result = ClassFactory.GetByteArrayValue(str);
                return((T)(object)result);
            }

            var result1 = (T)CustomClassFactory.ParseCustomClass(_nodeObject.FinishObj, type);
            return(result1);
        }