コード例 #1
0
        /*----------Functions----------*/
        //PRIVATE

        /// <summary>
        /// Check to see if the supplied type can be processed
        /// </summary>
        /// <param name="type">The type of object that is being tested</param>
        /// <returns>Returns true if the type can be processed</returns>
        private static bool CanProcessTypeValue(Type type)
        {
            //If the type is stored in the basic types lookup, its good
            if (BASIC_TYPE_LOOKUP.Contains(type))
            {
                return(true);
            }

            //Check the other basic types
            if (type == CHAR)
            {
                return(true);
            }
            if (UOBJ.IsAssignableFrom(type))
            {
                return(true);
            }
            if (ENUM.IsAssignableFrom(type))
            {
                return(true);
            }

            //Can't process a generic object only
            if (type == OBJ)
            {
                return(false);
            }

            //Otherwise check it is a value or has a default constructor
            return(type.IsValueType || type.GetConstructor(Type.EmptyTypes) != null);
        }
コード例 #2
0
        /// <summary>
        /// Retrieve the serialised value for a single object
        /// </summary>
        /// <param name="obj">The object that is to be serialsied</param>
        /// <param name="type">The type of object that is being serialised</param>
        /// <returns>Returns the value as a serialised string</returns>
        private static string SerialiseValue(object obj, Type type)
        {
            //If the object is a basic type, just to string it
            if (BASIC_TYPE_LOOKUP.Contains(type))
            {
                return(obj != null ? obj.ToString() : string.Empty);
            }

            //If this is a char, use the integral value
            if (type == CHAR)
            {
                return(((short)(char)obj).ToString());
            }

            //If a Unity object, serialise an object container
            if (UOBJ.IsAssignableFrom(type))
            {
                return(JsonUtility.ToJson(new SerialUnityObjectContainer {
                    obj = obj as UnityEngine.Object
                }));
            }

            //If the object is an enum
            if (ENUM.IsAssignableFrom(type))
            {
                return(JsonUtility.ToJson(new SerialCollectionContainer {
                    vals = new string[] {
                        MinifyTypeAssemblyName(type),
                        Convert.ToInt64(obj).ToString()
                    }
                }));
            }

            //At this point, use JsonUtility to manage other types
            return(JsonUtility.ToJson(obj));
        }
コード例 #3
0
        /// <summary>
        /// Parse the supplied text to an object of the specified type
        /// </summary>
        /// <param name="serial">The serialised data that is to be parsed to re-create the object definition</param>
        /// <param name="type">The type of the object that is to be returned by this operation</param>
        /// <returns>Returns a generic object of the specified type</returns>
        private static object ParseValue(string serial, Type type)
        {
            //Check if the object is one of the default types
            if (type == SBYTE)
            {
                sbyte val;    sbyte.TryParse(serial, out val);    return(val);
            }
            if (type == SHORT)
            {
                short val;    short.TryParse(serial, out val);    return(val);
            }
            if (type == INT)
            {
                int val;      int.TryParse(serial, out val);      return(val);
            }
            if (type == LONG)
            {
                long val;     long.TryParse(serial, out val);     return(val);
            }
            if (type == BYTE)
            {
                byte val;     byte.TryParse(serial, out val);     return(val);
            }
            if (type == USHORT)
            {
                ushort val;   ushort.TryParse(serial, out val);   return(val);
            }
            if (type == UINT)
            {
                uint val;     uint.TryParse(serial, out val);     return(val);
            }
            if (type == ULONG)
            {
                ulong val;    ulong.TryParse(serial, out val);    return(val);
            }
            if (type == FLOAT)
            {
                float val;    float.TryParse(serial, out val);    return(val);
            }
            if (type == DOUBLE)
            {
                double val;   double.TryParse(serial, out val);   return(val);
            }
            if (type == DECIMAL)
            {
                decimal val;  decimal.TryParse(serial, out val);  return(val);
            }
            if (type == BOOL)
            {
                bool val;     bool.TryParse(serial, out val);     return(val);
            }
            if (type == STRING)
            {
                return(serial);
            }
            if (type == CHAR)
            {
                short val;    short.TryParse(serial, out val);    return((char)val);
            }

            //If this is a Unity Engine object, deserialise the usual way
            if (UOBJ.IsAssignableFrom(type))
            {
                //Deserialise the object container
                SerialUnityObjectContainer objectContainer = JsonUtility.FromJson <SerialUnityObjectContainer>(serial);

                //Return the object reference value
                return(objectContainer.obj);
            }

            //If the object is an enumeration
            if (ENUM.IsAssignableFrom(type))
            {
                //Deserialise a container object
                SerialCollectionContainer container = JsonUtility.FromJson <SerialCollectionContainer>(serial);

                //There should be two values in the container
                if (container.vals.Length != 2)
                {
                    throw new ArgumentException("Serialised enumeration data has two values for processing. Received " + container.vals.Length);
                }

                //Determine the type that is being used
                Type enumType = Type.GetType(container.vals[0]);

                //Get the integral value to use
                long val = Convert.ToInt64(container.vals[1]);

                //Return the final value converted to the enumeration
                return(Enum.ToObject(enumType, val));
            }

            //At this point, use JsonUtility to manage other types
            return(JsonUtility.FromJson(serial, type));
        }