示例#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
 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);
         }
     }
 }