Ejemplo n.º 1
0
        public static T DeserializeGeneric <T>(string serializedClassWrapper)
        {
            SerializedClassWrapper serializedWrapper = JsonUtility.FromJson <SerializedClassWrapper>(serializedClassWrapper);

            if (serializedWrapper == null)
            {
                Debug.LogError("JsonSerialization - DeserializeGeneric failed to deserialize class wrapper!");
                return(default(T));
            }

            Type type = Type.GetType(serializedWrapper.typeName);

            MethodInfo genericMethod = typeof(JsonSerialization).GetMethod("DeserializeType", BindingFlags.Static | BindingFlags.NonPublic);
            MethodInfo method        = genericMethod.MakeGenericMethod(type);

            object obj          = method.Invoke(null, new object[] { serializedWrapper.serializedClass });
            T      castedObject = default(T);

            try {
                castedObject = (T)obj;
            } catch (InvalidCastException) {
                Debug.LogError("JsonSerialization - DeserializeGeneric failed to cast deserialized class!");
            }

            return(castedObject);
        }
Ejemplo n.º 2
0
        public static ITransitionCondition Deserialize(string serializedClassWrapper)
        {
            SerializedClassWrapper serializedConditionWrapper = JsonUtility.FromJson <SerializedClassWrapper>(serializedClassWrapper);

            if (serializedConditionWrapper == null)
            {
                Debug.LogError("TransitionConditionSerializer - Deserialize failed to deserialize class wrapper!");
                return(null);
            }

            Type type = Type.GetType(serializedConditionWrapper.typeName);

            MethodInfo genericMethod = typeof(TransitionConditionSerializer).GetMethod("DeserializeCondition", BindingFlags.Static | BindingFlags.NonPublic);
            MethodInfo method        = genericMethod.MakeGenericMethod(type);

            return(method.Invoke(null, new object[] { serializedConditionWrapper.serializedClass }) as ITransitionCondition);
        }
Ejemplo n.º 3
0
        public static string SerializeGeneric(object obj)
        {
            if (obj == null)
            {
                Debug.LogError("JsonSerialization - SerializeGeneric was passed in null!");
                return("");
            }

            Type type = obj.GetType();

            MethodInfo genericMethod = typeof(JsonSerialization).GetMethod("SerializeType", BindingFlags.Static | BindingFlags.NonPublic);
            MethodInfo method        = genericMethod.MakeGenericMethod(type);

            string serializedCondition = (string)method.Invoke(null, new object[] { obj });

            var serializedWrapper = new SerializedClassWrapper(type, serializedCondition);

            return(JsonUtility.ToJson(serializedWrapper, prettyPrint: true));
        }
Ejemplo n.º 4
0
        public static string Serialize(ITransitionCondition condition)
        {
            if (condition == null)
            {
                Debug.LogError("TransitionConditionSerializer - Serialize was passed in null condition!");
                return("");
            }

            Type type = condition.GetType();

            MethodInfo genericMethod = typeof(TransitionConditionSerializer).GetMethod("SerializeCondition", BindingFlags.Static | BindingFlags.NonPublic);
            MethodInfo method        = genericMethod.MakeGenericMethod(type);

            string serializedCondition = (string)method.Invoke(null, new object[] { condition });

            var serializedConditionWrapper = new SerializedClassWrapper(type, serializedCondition);

            return(JsonUtility.ToJson(serializedConditionWrapper, prettyPrint: true));
        }