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;
     }
 }
Exemple #2
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsPositiveFixInt)
     {
         return((ulong)reader.ReadPositiveFixInt(format));
     }
     if (format.IsUInt8)
     {
         return((ulong)reader.ReadUInt8());
     }
     if (format.IsUInt16)
     {
         return((ulong)reader.ReadUInt16());
     }
     if (format.IsUInt32)
     {
         return((ulong)reader.ReadUInt32());
     }
     if (format.IsUInt64)
     {
         return(reader.ReadUInt64());
     }
     if (format.IsNil)
     {
         return(default(ulong));
     }
     throw new FormatException(this, format, reader);
 }
Exemple #3
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsPositiveFixInt)
     {
         return((short)reader.ReadPositiveFixInt(format));
     }
     if (format.IsUInt8)
     {
         return((short)reader.ReadUInt8());
     }
     if (format.IsUInt16)
     {
         return(Convert.ToInt16(reader.ReadUInt16()));
     }
     if (format.IsNegativeFixInt)
     {
         return((short)reader.ReadNegativeFixInt(format));
     }
     if (format.IsInt8)
     {
         return((short)reader.ReadInt8());
     }
     if (format.IsInt16)
     {
         return(reader.ReadInt16());
     }
     if (format.IsNil)
     {
         return(default(short));
     }
     throw new FormatException(this, format, reader);
 }
        object ReadExt(Format format, FormatReader reader)
        {
            uint  length  = reader.ReadExtLength(format);
            sbyte extType = reader.ReadExtType(format);

            return(context.TypeHandlers.GetExt(extType).ReadExt(length, reader));
        }
 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);
 }
 JsonConverter(Stream stream, SerializationContext context = null)
 {
     this.context         = context ?? SerializationContext.Default;
     this.reader          = new FormatReader(stream);
     this.builder         = new StringBuilder();
     this.indentationSize = 0;
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsNil)
     {
         return(null);
     }
     return(underlyingTypeHandler.Read(format, reader));
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsNil)
     {
         return(null);
     }
     return(new Uri((string)GetStringHandler().Read(format, reader)));
 }
 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);
 }
Exemple #10
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);
 }
 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 Read(Format format, FormatReader reader)
 {
     if (format.IsBin8)
     {
         binaryHandler = binaryHandler ?? context.TypeHandlers.Get <byte[]>();
         return(new Guid((byte[])binaryHandler.Read(format, reader)));
     }
     if (format.IsStr8)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         return(new Guid((string)stringHandler.Read(format, reader)));
     }
     throw new FormatException(this, format, reader);
 }
Exemple #13
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsPositiveFixInt)
     {
         return(reader.ReadPositiveFixInt(format));
     }
     if (format.IsUInt8)
     {
         return(reader.ReadUInt8());
     }
     if (format.IsNil)
     {
         return(default(byte));
     }
     throw new FormatException(this, format, reader);
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsFalse)
     {
         return(false);
     }
     if (format.IsTrue)
     {
         return(true);
     }
     if (format.IsNil)
     {
         return(default(bool));
     }
     throw new FormatException(this, format, reader);
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsIntFamily)
     {
         intHandler = intHandler ?? context.TypeHandlers.Get <int>();
         return(Enum.ToObject(type, intHandler.Read(format, reader)));
     }
     if (format.IsStringFamily)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         return(Enum.Parse(type, (string)stringHandler.Read(format, reader), true));
     }
     if (format.IsNil)
     {
         return(Enum.ToObject(type, 0));
     }
     throw new FormatException(this, format, reader);
 }
Exemple #16
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);
        }
        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);
        }
Exemple #18
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsNil)
     {
         return(new byte[0]);
     }
     if (format.IsBin8)
     {
         return(reader.ReadBin8());
     }
     if (format.IsBin16)
     {
         return(reader.ReadBin16());
     }
     if (format.IsBin32)
     {
         return(reader.ReadBin32());
     }
     throw new FormatException(this, format, reader);
 }
 public object ReadExt(uint length, FormatReader reader)
 {
     // Timestamp 32
     if (length == 4)
     {
         return(epoch.AddSeconds(reader.ReadUInt32()).ToLocalTime());
     }
     // Timestamp 64
     if (length == 8)
     {
         byte[] buffer      = reader.ReadBytesOfLength(8);
         uint   nanoseconds = ((uint)buffer[0] << 22) | ((uint)buffer[1] << 14) | ((uint)buffer[2] << 6) | (uint)buffer[3] >> 2;
         ulong  seconds     = ((ulong)(buffer[3] & 0x3) << 32) | ((ulong)buffer[4] << 24) | ((ulong)buffer[5] << 16) | ((ulong)buffer[6] << 8) | (ulong)buffer[7];
         return(epoch.AddTicks(nanoseconds / 100).AddSeconds(seconds).ToLocalTime());
     }
     // Timestamp 96
     if (length == 12)
     {
         return(epoch.AddTicks(reader.ReadUInt32() / 100).AddSeconds(reader.ReadInt64()).ToLocalTime());
     }
     throw new FormatException();
 }
Exemple #20
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);
 }
 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);
 }
Exemple #22
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);
        }
Exemple #23
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsFixStr)
     {
         return(reader.ReadFixStr(format));
     }
     if (format.IsStr8)
     {
         return(reader.ReadStr8());
     }
     if (format.IsStr16)
     {
         return(reader.ReadStr16());
     }
     if (format.IsStr32)
     {
         return(reader.ReadStr32());
     }
     if (format.IsNil)
     {
         return(null);
     }
     throw new FormatException(this, format, reader);
 }
Exemple #24
0
 public FormatException(ITypeHandler handler, Format format, FormatReader reader) : base(string.Format("{0}: Undefined Format {1} at position {2}", handler.GetType(), format, reader.Position))
 {
 }
 object ReadArray(Format format, FormatReader reader)
 {
     return(context.TypeHandlers.Get <List <object> >().Read(format, reader));
 }
 public object Read(Format format, FormatReader reader)
 {
     longHandler = longHandler ?? context.TypeHandlers.Get <long>();
     return(new TimeSpan((long)longHandler.Read(format, reader)));
 }
Exemple #27
0
 public object Read(Format format, FormatReader reader)
 {
     intArrayHandler = intArrayHandler ?? context.TypeHandlers.Get <int[]>();
     int[] bits = (int[])intArrayHandler.Read(format, reader);
     return(new decimal(bits));
 }
 object ReadMap(Format format, FormatReader reader)
 {
     return(context.TypeHandlers.Get <Dictionary <object, object> >().Read(format, reader));
 }
Exemple #29
0
 public FormatException(Format format, FormatReader reader) : base(string.Format("Undefined Format {0} at Position: {1}", format, reader.Position))
 {
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsNil)
     {
         return(null);
     }
     if (format.IsFalse)
     {
         return(false);
     }
     if (format.IsTrue)
     {
         return(true);
     }
     if (format.IsPositiveFixInt)
     {
         return(reader.ReadPositiveFixInt(format));
     }
     if (format.IsUInt8)
     {
         return(reader.ReadUInt8());
     }
     if (format.IsUInt16)
     {
         return(reader.ReadUInt16());
     }
     if (format.IsUInt32)
     {
         return(reader.ReadUInt32());
     }
     if (format.IsUInt64)
     {
         return(reader.ReadUInt64());
     }
     if (format.IsNegativeFixInt)
     {
         return(reader.ReadNegativeFixInt(format));
     }
     if (format.IsInt8)
     {
         return(reader.ReadInt8());
     }
     if (format.IsInt16)
     {
         return(reader.ReadInt16());
     }
     if (format.IsInt32)
     {
         return(reader.ReadInt32());
     }
     if (format.IsInt64)
     {
         return(reader.ReadInt64());
     }
     if (format.IsFloat32)
     {
         return(reader.ReadFloat32());
     }
     if (format.IsFloat64)
     {
         return(reader.ReadFloat64());
     }
     if (format.IsFixStr)
     {
         return(reader.ReadFixStr(format));
     }
     if (format.IsStr8)
     {
         return(reader.ReadStr8());
     }
     if (format.IsStr16)
     {
         return(reader.ReadStr16());
     }
     if (format.IsStr32)
     {
         return(reader.ReadStr32());
     }
     if (format.IsBin8)
     {
         return(reader.ReadBin8());
     }
     if (format.IsBin16)
     {
         return(reader.ReadBin16());
     }
     if (format.IsBin32)
     {
         return(reader.ReadBin32());
     }
     if (format.IsArrayFamily)
     {
         return(ReadArray(format, reader));
     }
     if (format.IsMapFamily)
     {
         return(ReadMap(format, reader));
     }
     if (format.IsExtFamily)
     {
         return(ReadExt(format, reader));
     }
     throw new FormatException(this, format, reader);
 }