Esempio n. 1
0
        // We will have problem with binary blobs greater than int.MaxValue bytes.
        public byte[] Read(IMsgPackReader reader, MsgPackContext context, Func <byte[]> creator)
        {
            var type = reader.ReadDataType();

            uint length;

            switch (type)
            {
            case DataTypes.Null:
                return(null);

            case DataTypes.Bin8:
                length = IntConverter.ReadUInt8(reader);
                break;

            case DataTypes.Bin16:
                length = IntConverter.ReadUInt16(reader);
                break;

            case DataTypes.Bin32:
                length = IntConverter.ReadUInt32(reader);
                break;

            default:
                throw ExceptionUtils.BadTypeException(type, DataTypes.Bin8, DataTypes.Bin16, DataTypes.Bin32, DataTypes.Null);
            }

            return(ReadByteArray(reader, length));
        }
        public override TArray Read(IMsgPackReader reader, MsgPackContext context, Func <TArray> creator)
        {
            var type = reader.ReadDataType();

            switch (type)
            {
            case DataTypes.Null:
                return(default(TArray));

            case DataTypes.Array16:
                return(ReadArray(reader, context, creator, IntConverter.ReadUInt16(reader)));

            case DataTypes.Array32:
                return(ReadArray(reader, context, creator, IntConverter.ReadUInt32(reader)));
            }

            uint length;

            if (TryGetLengthFromFixArray(type, out length))
            {
                return(ReadArray(reader, context, creator, length));
            }

            throw ExceptionUtils.BadTypeException(type, DataTypes.Array16, DataTypes.Array32, DataTypes.FixArray);
        }
Esempio n. 3
0
        public string Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            switch (type)
            {
            case DataTypes.Null:
                return(null);

            case DataTypes.Str8:
                return(ReadString(reader, NumberConverter.ReadUInt8(reader)));

            case DataTypes.Str16:
                return(ReadString(reader, NumberConverter.ReadUInt16(reader)));

            case DataTypes.Str32:
                return(ReadString(reader, NumberConverter.ReadUInt32(reader)));
            }

            uint length;

            if (TryGetFixstrLength(type, out length))
            {
                return(ReadString(reader, length));
            }

            throw ExceptionUtils.BadTypeException(type, DataTypes.FixStr, DataTypes.Str8, DataTypes.Str16, DataTypes.Str32);
        }
Esempio n. 4
0
        public IndexPart Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            switch (type)
            {
            case DataTypes.Map16:
                return(ReadFromMap(reader, ReadUInt16(reader)));

            case DataTypes.Map32:
                return(ReadFromMap(reader, ReadUInt32(reader)));

            case DataTypes.Array16:
                return(ReadFromArray(reader, ReadUInt16(reader)));

            case DataTypes.Array32:
                return(ReadFromArray(reader, ReadUInt32(reader)));
            }

            var length = TryGetLengthFromFixMap(type);

            if (length.HasValue)
            {
                return(ReadFromMap(reader, length.Value));
            }

            length = TryGetLengthFromFixArray(type);
            if (length != null)
            {
                return(ReadFromArray(reader, length.Value));
            }

            throw ExceptionUtils.BadTypeException(type, DataTypes.Map16, DataTypes.Map32, DataTypes.FixMap, DataTypes.Array16, DataTypes.Array32, DataTypes.FixArray);
        }
Esempio n. 5
0
        public object Read(IMsgPackReader reader, MsgPackContext context, Func <object> creator)
        {
            var type = reader.ReadDataType();

            if (type == DataTypes.Null)
            {
                return(null);
            }

            throw ExceptionUtils.BadTypeException(type, DataTypes.Null);
        }
Esempio n. 6
0
        public object Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            if (type == DataTypes.Null)
            {
                return(null);
            }

            throw ExceptionUtils.BadTypeException(type, DataTypes.Null);
        }
Esempio n. 7
0
        float IMsgPackConverter <float> .Read(IMsgPackReader reader, MsgPackContext context, Func <float> creator)
        {
            var type = reader.ReadDataType();

            if (type != DataTypes.Single)
            {
                throw ExceptionUtils.BadTypeException(type, DataTypes.Single);
            }

            return(ReadFloat(reader));
        }
Esempio n. 8
0
        public bool Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            switch (type)
            {
            case DataTypes.True:
                return(true);

            case DataTypes.False:
                return(false);

            default:
                throw ExceptionUtils.BadTypeException(type, DataTypes.True, DataTypes.False);
            }
        }
Esempio n. 9
0
        float IMsgPackConverter <float> .Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            if (type == DataTypes.Single)
            {
                return(ReadFloat(reader));
            }

            if (_strictParseOfFloat)
            {
                throw ExceptionUtils.BadTypeException(type, DataTypes.Single);
            }

            return(TryGetInt32(type, reader) ?? throw ExceptionUtils.BadTypeException(type, DataTypes.Single));
        }
Esempio n. 10
0
        public double Read(IMsgPackReader reader, MsgPackContext context, Func <double> creator)
        {
            var type = reader.ReadDataType();

            if (type != DataTypes.Single && type != DataTypes.Double)
            {
                throw ExceptionUtils.BadTypeException(type, DataTypes.Single, DataTypes.Double);
            }

            if (type == DataTypes.Single)
            {
                return(ReadFloat(reader));
            }

            var bytes = ReadBytes(reader, 8);

            return(new DoubleBinary(bytes).value);
        }
Esempio n. 11
0
        double IMsgPackConverter <double> .Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            if (type != DataTypes.Single && type != DataTypes.Double)
            {
                if (_strictParseOfFloat)
                {
                    throw ExceptionUtils.BadTypeException(type, DataTypes.Single, DataTypes.Double);
                }

                return(TryGetInt64(type, reader) ?? throw ExceptionUtils.BadTypeException(type, DataTypes.Single, DataTypes.Double));
            }

            if (type == DataTypes.Single)
            {
                return(ReadFloat(reader));
            }

            var bytes = ReadBytes(reader, 8);

            return(new DoubleBinary(bytes).value);
        }
Esempio n. 12
0
        public void TestDoubleStritctParsing(byte[] bytes)
        {
            var e = Should.Throw <SerializationException>(() => MsgPackSerializer.Deserialize <double>(bytes, new MsgPackContext(true)));

            e.Message.ShouldBe(ExceptionUtils.BadTypeException((DataTypes)bytes[0], DataTypes.Single, DataTypes.Double).Message);
        }
Esempio n. 13
0
        // We will have problem with binary blobs greater than int.MaxValue bytes.
        public byte[] Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            uint length;

            switch (type)
            {
            case DataTypes.Null:
                return(null);

            case DataTypes.Bin8:
                length = NumberConverter.ReadUInt8(reader);
                break;

            case DataTypes.Bin16:
                length = NumberConverter.ReadUInt16(reader);
                break;

            case DataTypes.Bin32:
                length = NumberConverter.ReadUInt32(reader);
                break;

            case DataTypes.Str8:
                if (_compatibilityMode)
                {
                    length = NumberConverter.ReadUInt8(reader);
                }
                else
                {
                    throw ExceptionUtils.CantReadStringAsBinary();
                }
                break;

            case DataTypes.Str16:
                if (_compatibilityMode)
                {
                    length = NumberConverter.ReadUInt16(reader);
                }
                else
                {
                    throw ExceptionUtils.CantReadStringAsBinary();
                }
                break;

            case DataTypes.Str32:
                if (_compatibilityMode)
                {
                    length = NumberConverter.ReadUInt32(reader);
                }
                else
                {
                    throw ExceptionUtils.CantReadStringAsBinary();
                }
                break;

            default:
                if ((type & DataTypes.FixStr) == DataTypes.FixStr)
                {
                    if (_compatibilityMode)
                    {
                        length = (uint)(type & ~DataTypes.FixStr);
                    }
                    else
                    {
                        throw ExceptionUtils.CantReadStringAsBinary();
                    }
                }
                else
                {
                    throw ExceptionUtils.BadTypeException(type, DataTypes.Bin8, DataTypes.Bin16, DataTypes.Bin32, DataTypes.Null);
                }
                break;
            }

            var segment = reader.ReadBytes(length);
            var array   = new byte[segment.Count];

            Array.Copy(segment.Array, segment.Offset, array, 0, segment.Count);
            return(array);
        }