예제 #1
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         floatHandler = floatHandler ?? context.TypeHandlers.Get <float>();
         int     length = reader.ReadArrayLength(format);
         float[] bytes  = new float[length];
         for (int i = 0; i < length; i++)
         {
             bytes[i] = (float)floatHandler.Read(reader.ReadFormat(), reader);
         }
         return(new Color(bytes[0], bytes[1], bytes[2], bytes[3]));
     }
     if (format.IsStringFamily)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         Color color;
         ColorUtility.TryParseHtmlString((string)stringHandler.Read(format, reader), out color);
         return(color);
     }
     if (format.IsMapFamily)
     {
         mapHandler = mapHandler ?? context.TypeHandlers.Get <Dictionary <string, float> >();
         Dictionary <string, float> map = (Dictionary <string, float>)mapHandler.Read(format, reader);
         return(new Color(map["r"], map["g"], map["b"], map["a"]));
     }
     throw new FormatException(this, format, reader);
 }
예제 #2
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         int   size  = reader.ReadArrayLength(format);
         Array array = Array.CreateInstance(elementType, size);
         for (int i = 0; i < size; i++)
         {
             object value = elementTypeHandler.Read(reader.ReadFormat(), reader);
             array.SetValue(value, i);
         }
         return(array);
     }
     if (format.IsNil)
     {
         if (context.ArrayOptions.NullAsEmptyOnUnpack)
         {
             return(Array.CreateInstance(elementType, 0));
         }
         return(null);
     }
     throw new FormatException(this, format, reader);
 }
예제 #3
0
        void ReadArray(Format format)
        {
            int size = reader.ReadArrayLength(format);

            if (size == 0)
            {
                Append("[]");
                return;
            }
            Append("[").ValueSeparator();
            indentationSize += 1;
            for (int i = 0; i < size; i++)
            {
                Indent().AppendStream();
                if (i < size - 1)
                {
                    Append(",");
                }
                ValueSeparator();
            }
            indentationSize -= 1;
            Indent().Append("]");
        }
예제 #4
0
        public object Read(Format format, FormatReader reader)
        {
            Type listType = typeof(List <>).MakeGenericType(new[] { innerType });

            if (format.IsArrayFamily)
            {
                IList list = (IList)Activator.CreateInstance(listType);
                int   size = reader.ReadArrayLength(format);
                for (int i = 0; i < size; i++)
                {
                    list.Add(innerTypeHandler.Read(reader.ReadFormat(), reader));
                }
                return(list);
            }
            if (format.IsNil)
            {
                if (context.ArrayOptions.NullAsEmptyOnUnpack)
                {
                    return(Activator.CreateInstance(listType));
                }
                return(null);
            }
            throw new FormatException(this, format, reader);
        }