Ejemplo n.º 1
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         floatHandler = floatHandler ?? context.TypeHandlers.Get <float>();
         Vector2 vector = new Vector2();
         vector.x = (float)floatHandler.Read(reader.ReadFormat(), reader);
         vector.y = (float)floatHandler.Read(reader.ReadFormat(), reader);
         return(vector);
     }
     throw new FormatException(this, format, reader);
 }
Ejemplo n.º 2
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         intHandler = intHandler ?? context.TypeHandlers.Get <int>();
         Vector3Int vector = new Vector3Int();
         vector.x = (int)intHandler.Read(reader.ReadFormat(), reader);
         vector.y = (int)intHandler.Read(reader.ReadFormat(), reader);
         vector.z = (int)intHandler.Read(reader.ReadFormat(), reader);
         return(vector);
     }
     throw new FormatException(this, format, reader);
 }
Ejemplo n.º 3
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         floatHandler = floatHandler ?? context.TypeHandlers.Get <float>();
         Quaternion quaternion = new Quaternion();
         quaternion.x = (float)floatHandler.Read(reader.ReadFormat(), reader);
         quaternion.y = (float)floatHandler.Read(reader.ReadFormat(), reader);
         quaternion.z = (float)floatHandler.Read(reader.ReadFormat(), reader);
         quaternion.w = (float)floatHandler.Read(reader.ReadFormat(), reader);
         return(quaternion);
     }
     throw new FormatException(this, format, reader);
 }
 public object Deserialize(Type type, Stream stream)
 {
     try {
         FormatReader reader = new FormatReader(stream);
         return(Context.TypeHandlers.Get(type).Read(reader.ReadFormat(), reader));
     }
     catch (FormatException exception) {
         if (stream.CanSeek)
         {
             MemoryStream partial = new MemoryStream((int)stream.Position);
             byte[]       buffer  = new byte[1024 * 16];
             stream.Position = 0;
             int remainingBytes = partial.Capacity;
             while (remainingBytes > 0)
             {
                 int bytesRead = stream.Read(buffer, 0, buffer.Length);
                 partial.Write(buffer, 0, bytesRead);
                 remainingBytes -= bytesRead;
             }
             partial.Position = 0;
             exception.Source = JsonConverter.Encode(partial);
             partial.Close();
         }
         throw;
     }
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
        public object Read(Format format, FormatReader reader)
        {
            IDictionary dictionary = (IDictionary)Activator.CreateInstance(type);

            if (format.IsNil)
            {
                return(dictionary);
            }
            int size = reader.ReadMapLength(format);

            while (size > 0)
            {
                object key   = keyHandler.Read(reader.ReadFormat(), reader);
                object value = valueHandler.Read(reader.ReadFormat(), reader);
                dictionary.Add(key, value);
                size = size - 1;
            }
            return(dictionary);
        }
Ejemplo n.º 7
0
        public object Read(Format format, FormatReader reader)
        {
            MapDefinition definition = lazyDefinition.Value;

            if (format.IsMapFamily)
            {
                object obj = Activator.CreateInstance(definition.Type);
                InvokeCallback <OnDeserializingAttribute>(obj, definition);
                int size = reader.ReadMapLength(format);
                while (size > 0)
                {
                    string name = (string)nameHandler.Read(reader.ReadFormat(), reader);
                    name = nameConverter.OnUnpack(name, definition);

                    if (definition.FieldHandlers.ContainsKey(name))
                    {
                        object value = definition.FieldHandlers[name].Read(reader.ReadFormat(), reader);
                        definition.FieldInfos[name].SetValue(obj, value);
                    }
                    else if (context.MapOptions.IgnoreUnknownFieldOnUnpack)
                    {
                        reader.Skip();
                    }
                    else
                    {
                        throw new MissingFieldException(name + " does not exist for type: " + definition.Type);
                    }
                    size = size - 1;
                }
                InvokeCallback <OnDeserializedAttribute>(obj, definition);
                return(obj);
            }
            if (format.IsEmptyArray && context.MapOptions.AllowEmptyArrayOnUnpack)
            {
                return(Activator.CreateInstance(definition.Type));
            }
            if (format.IsNil)
            {
                return(null);
            }
            throw new FormatException(this, format, reader);
        }
Ejemplo n.º 8
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);
 }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsExtFamily)
     {
         uint length = reader.ReadExtLength(format);
         if (ExtType == reader.ReadExtType(reader.ReadFormat()))
         {
             return(ReadExt(length, reader));
         }
     }
     if (format.IsStringFamily)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         string dateTimeStr = (string)stringHandler.Read(format, reader);
         return(DateTime.Parse(dateTimeStr));
     }
     if (format.IsFloatFamily || format.IsIntFamily)
     {
         doubleHandler = doubleHandler ?? context.TypeHandlers.Get <double>();
         double seconds = (double)doubleHandler.Read(format, reader);
         return(epoch.AddSeconds(seconds).ToLocalTime());
     }
     throw new FormatException(this, format, reader);
 }
Ejemplo n.º 11
0
        JsonConverter AppendStream()
        {
            Format format = reader.ReadFormat();

            if (format.IsNil)
            {
                Append("null");
            }
            else if (format.IsFalse)
            {
                Append("false");
            }
            else if (format.IsTrue)
            {
                Append("true");
            }
            else if (format.IsPositiveFixInt)
            {
                Append(reader.ReadPositiveFixInt(format).ToString());
            }
            else if (format.IsUInt8)
            {
                Append(reader.ReadUInt8().ToString());
            }
            else if (format.IsUInt16)
            {
                Append(reader.ReadUInt16().ToString());
            }
            else if (format.IsUInt32)
            {
                Append(reader.ReadUInt32().ToString());
            }
            else if (format.IsUInt64)
            {
                Append(reader.ReadUInt64().ToString());
            }
            else if (format.IsNegativeFixInt)
            {
                Append(reader.ReadNegativeFixInt(format).ToString());
            }
            else if (format.IsInt8)
            {
                Append(reader.ReadInt8().ToString());
            }
            else if (format.IsInt16)
            {
                Append(reader.ReadInt16().ToString());
            }
            else if (format.IsInt32)
            {
                Append(reader.ReadInt32().ToString());
            }
            else if (format.IsInt64)
            {
                Append(reader.ReadInt64().ToString());
            }
            else if (format.IsFloat32)
            {
                Append(reader.ReadFloat32().ToString());
            }
            else if (format.IsFloat64)
            {
                Append(reader.ReadFloat64().ToString());
            }
            else if (format.IsFixStr)
            {
                AppendQuotedString(reader.ReadFixStr(format));
            }
            else if (format.IsStr8)
            {
                AppendQuotedString(reader.ReadStr8());
            }
            else if (format.IsStr16)
            {
                AppendQuotedString(reader.ReadStr16());
            }
            else if (format.IsStr32)
            {
                AppendQuotedString(reader.ReadStr32());
            }
            else if (format.IsBin8)
            {
                StringifyBinary(reader.ReadBin8());
            }
            else if (format.IsBin16)
            {
                StringifyBinary(reader.ReadBin16());
            }
            else if (format.IsBin32)
            {
                StringifyBinary(reader.ReadBin32());
            }
            else if (format.IsArrayFamily)
            {
                ReadArray(format);
            }
            else if (format.IsMapFamily)
            {
                ReadMap(format);
            }
            else if (format.IsExtFamily)
            {
                ReadExt(format);
            }
            else
            {
                throw new FormatException(format, reader);
            }

            return(this);
        }