示例#1
0
        static bool SerializeObject(object obj, StringBuilder builder)
        {
            var enumerator = AOT.GetDictionaryEnumerator(obj);

            if (enumerator == null)
            {
                EB.Debug.LogError("Object does not implement IDictionaryEnumerator interface: " + obj.GetType());
                return(false);
            }

            builder.Append("{");

            bool first  = true;
            bool result = true;

            while (enumerator.MoveNext())
            {
                string key   = enumerator.Key.ToString();
                object value = enumerator.Value;

                if (!first)
                {
                    builder.Append(",");
                }

                SerializeString(key, builder);
                builder.Append(":");

                if (!SerializeValue(value, builder))
                {
                    builder.Append("null");
                    result = false;
                }

                first = false;
            }

            // cleanup
            if (enumerator is System.IDisposable)
            {
                var disposable = (System.IDisposable)enumerator;
                disposable.Dispose();
            }

            builder.Append("}");

            return(result);
        }
示例#2
0
        // Trampoline safe conversion to list.
        public static List <T> ToList <T>(object array)
        {
            List <T> list = new List <T>();

            IEnumerator enumerator = AOT.GetEnumerator(array);

            if (enumerator != null)
            {
                while (enumerator.MoveNext())
                {
                    list.Add((T)enumerator.Current);
                }
            }

            return(list);
        }
示例#3
0
        static bool SerializeArray(object arr, StringBuilder builder)
        {
            var enumerator = AOT.GetEnumerator(arr);

            if (enumerator == null)
            {
                EB.Debug.LogError("Object does not implement IEnumerable interface: " + arr.GetType());
                return(false);
            }

            builder.Append("[");

            bool result = true;
            bool first  = true;

            while (enumerator.MoveNext())
            {
                object value = enumerator.Current;

                if (!first)
                {
                    builder.Append(",");
                }

                if (!SerializeValue(value, builder))
                {
                    builder.Append("null");
                    result = false;
                }

                first = false;
            }

            // cleanup
            if (enumerator is System.IDisposable)
            {
                var disposable = (System.IDisposable)enumerator;
                disposable.Dispose();
            }

            builder.Append("]");

            return(result);
        }
示例#4
0
        public static string Join(object list, char separator)
        {
            var sb         = new System.Text.StringBuilder();
            var add        = false;
            var enumerator = AOT.GetEnumerator(list);

            if (enumerator != null)
            {
                while (enumerator.MoveNext())
                {
                    var obj = enumerator.Current;
                    if (obj != null)
                    {
                        if (add)
                        {
                            sb.Append(separator);
                        }
                        sb.Append(obj.ToString());
                        add = true;
                    }
                }
            }
            return(sb.ToString());
        }
示例#5
0
 void Hash(Digest digest, object value, bool deep)
 {
     if (value != null)
     {
         var type = value.GetType();
         if (type.IsArray || (value is IList))
         {
             var enumerator = AOT.GetEnumerator(value);
             if (enumerator != null)
             {
                 while (enumerator.MoveNext())
                 {
                     Hash(digest, enumerator.Current, deep);
                 }
             }
         }
         else if (value is IDictionary)
         {
             var enumerator = AOT.GetDictionaryEnumerator(value);
             if (enumerator != null)
             {
                 while (enumerator.MoveNext())
                 {
                     Hash(digest, enumerator.Key, deep);
                     Hash(digest, enumerator.Value, deep);
                 }
             }
         }
         else if (type == typeof(int))
         {
             digest.Update(System.BitConverter.GetBytes((int)value));
         }
         else if (type == typeof(uint))
         {
             digest.Update(System.BitConverter.GetBytes((uint)value));
         }
         else if (type == typeof(bool))
         {
             digest.Update(System.BitConverter.GetBytes((bool)value));
         }
         else if (type == typeof(ushort))
         {
             digest.Update(System.BitConverter.GetBytes((ushort)value));
         }
         else if (type == typeof(short))
         {
             digest.Update(System.BitConverter.GetBytes((short)value));
         }
         else if (type == typeof(float))
         {
             digest.Update(System.BitConverter.GetBytes((float)value));
         }
         else if (type == typeof(double))
         {
             digest.Update(System.BitConverter.GetBytes((double)value));
         }
         else if (type == typeof(ulong))
         {
             digest.Update(System.BitConverter.GetBytes((ulong)value));
         }
         else if (type == typeof(long))
         {
             digest.Update(System.BitConverter.GetBytes((uint)value));
         }
         else if (type == typeof(string))
         {
             digest.Update(Encoding.GetBytes((string)value));
         }
         else if (type.IsEnum)
         {
             digest.Update(System.BitConverter.GetBytes(System.Convert.ToInt32(value)));
         }
         else if (type.IsClass && deep)
         {
             var info = GetTypeInfo(type);
             info._GetHash(digest, value, deep);
         }
     }
 }