public IDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var formatter   = formatterResolver.GetFormatterWithVerify <object>();
            var startOffset = offset;

            var count = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);

            offset += readSize;

            var dict = new Dictionary <object, object>(count, MessagePackSecurity.Active.GetEqualityComparer <object>());

            using (MessagePackSecurity.DepthStep())
            {
                for (int i = 0; i < count; i++)
                {
                    var key = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;
                    var value = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;
                    dict.Add(key, value);
                }
            }

            readSize = offset - startOffset;
            return(dict);
        }
Beispiel #2
0
        public Int16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            else
            {
                var startOffset = offset;

                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;
                var array = new Int16[len];
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        array[i] = MessagePackBinary.ReadInt16(bytes, offset, out readSize);
                        offset  += readSize;
                    }
                }

                readSize = offset - startOffset;
                return(array);
            }
        }
        public Tuple <T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            else
            {
                var startOffset = offset;
                var count       = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                if (count != 1)
                {
                    throw new InvalidOperationException("Invalid Tuple count");
                }
                offset += readSize;
                using (MessagePackSecurity.DepthStep())
                {
                    var item1 = formatterResolver.GetFormatterWithVerify <T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;

                    readSize = offset - startOffset;
                    return(new Tuple <T1>(item1));
                }
            }
        }
        public IList Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(default(IList));
            }

            var formatter   = formatterResolver.GetFormatterWithVerify <object>();
            var startOffset = offset;

            var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            var list = new object[count];

            using (MessagePackSecurity.DepthStep())
            {
                for (int i = 0; i < count; i++)
                {
                    list[i] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;
                }
            }

            readSize = offset - startOffset;
            return(list);
        }
        public IGrouping <TKey, TElement> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            else
            {
                var startOffset = offset;
                var count       = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;

                if (count != 2)
                {
                    throw new InvalidOperationException("Invalid Grouping format.");
                }

                using (MessagePackSecurity.DepthStep())
                {
                    var key = formatterResolver.GetFormatterWithVerify <TKey>().Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;

                    var value = formatterResolver.GetFormatterWithVerify <IEnumerable <TElement> >().Deserialize(bytes, offset, formatterResolver, out readSize);
                    offset += readSize;

                    readSize = offset - startOffset;
                    return(new Grouping <TKey, TElement>(key, value));
                }
            }
        }
        public TCollection Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(default(TCollection));
            }
            else
            {
                var startOffset = offset;
                var formatter   = formatterResolver.GetFormatterWithVerify <TElement>();

                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;

                var list = Create(len);
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < len; i++)
                    {
                        Add(list, i, formatter.Deserialize(bytes, offset, formatterResolver, out readSize));
                        offset += readSize;
                    }
                }

                readSize = offset - startOffset;

                return(Complete(list));
            }
        }
        public List <T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            else
            {
                var startOffset = offset;
                var formatter   = formatterResolver.GetFormatterWithVerify <T>();

                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;
                var list = new List <T>(len);
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < len; i++)
                    {
                        list.Add(formatter.Deserialize(bytes, offset, formatterResolver, out readSize));
                        offset += readSize;
                    }
                }

                readSize = offset - startOffset;
                return(list);
            }
        }
Beispiel #8
0
        public T[,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }
            else
            {
                var startOffset = offset;
                var formatter   = formatterResolver.GetFormatterWithVerify <T>();

                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;
                if (len != ArrayLength)
                {
                    throw new InvalidOperationException("Invalid T[,] format");
                }

                var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                offset += readSize;

                var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
                offset += readSize;

                var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;

                var array = new T[iLength, jLength];

                var i = 0;
                var j = -1;
                using (MessagePackSecurity.DepthStep())
                {
                    for (int loop = 0; loop < maxLen; loop++)
                    {
                        if (j < jLength - 1)
                        {
                            j++;
                        }
                        else
                        {
                            j = 0;
                            i++;
                        }

                        array[i, j] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                        offset     += readSize;
                    }
                }

                readSize = offset - startOffset;
                return(array);
            }
        }
Beispiel #9
0
 public Lazy <T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
 {
     if (MessagePackBinary.IsNil(bytes, offset))
     {
         readSize = 1;
         return(null);
     }
     else
     {
         using (MessagePackSecurity.DepthStep())
         {
             // deserialize immediately(no delay, because capture byte[] causes memory leak)
             var v = formatterResolver.GetFormatterWithVerify <T>().Deserialize(bytes, offset, formatterResolver, out readSize);
             return(new Lazy <T>(() => v));
         }
     }
 }
Beispiel #10
0
        public KeyValuePair <TKey, TValue> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var startOffset = offset;
            var count       = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);

            offset += readSize;

            if (count != 2)
            {
                throw new InvalidOperationException("Invalid KeyValuePair format.");
            }

            using (MessagePackSecurity.DepthStep())
            {
                var key = formatterResolver.GetFormatterWithVerify <TKey>().Deserialize(bytes, offset, formatterResolver, out readSize);
                offset += readSize;

                var value = formatterResolver.GetFormatterWithVerify <TValue>().Deserialize(bytes, offset, formatterResolver, out readSize);
                offset += readSize;

                readSize = offset - startOffset;
                return(new KeyValuePair <TKey, TValue>(key, value));
            }
        }
Beispiel #11
0
        public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(default(TDictionary));
            }
            else
            {
                var startOffset    = offset;
                var keyFormatter   = formatterResolver.GetFormatterWithVerify <TKey>();
                var valueFormatter = formatterResolver.GetFormatterWithVerify <TValue>();

                var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
                offset += readSize;

                var dict = Create(len);
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < len; i++)
                    {
                        var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                        offset += readSize;

                        var value = valueFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                        offset += readSize;

                        Add(dict, i, key, value);
                    }
                }

                readSize = offset - startOffset;

                return(Complete(dict));
            }
        }
Beispiel #12
0
        public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var type = MessagePackBinary.GetMessagePackType(bytes, offset);

            switch (type)
            {
            case MessagePackType.Integer:
                var code = bytes[offset];
                if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt)
                {
                    return(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt)
                {
                    return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int8)
                {
                    return(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int16)
                {
                    return(MessagePackBinary.ReadInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int32)
                {
                    return(MessagePackBinary.ReadInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.Int64)
                {
                    return(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt8)
                {
                    return(MessagePackBinary.ReadByte(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt16)
                {
                    return(MessagePackBinary.ReadUInt16(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt32)
                {
                    return(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
                }
                else if (code == MessagePackCode.UInt64)
                {
                    return(MessagePackBinary.ReadUInt64(bytes, offset, out readSize));
                }
                throw new InvalidOperationException("Invalid primitive bytes.");

            case MessagePackType.Boolean:
                return(MessagePackBinary.ReadBoolean(bytes, offset, out readSize));

            case MessagePackType.Float:
                if (MessagePackCode.Float32 == bytes[offset])
                {
                    return(MessagePackBinary.ReadSingle(bytes, offset, out readSize));
                }
                else
                {
                    return(MessagePackBinary.ReadDouble(bytes, offset, out readSize));
                }

            case MessagePackType.String:
                return(MessagePackBinary.ReadString(bytes, offset, out readSize));

            case MessagePackType.Binary:
                return(MessagePackBinary.ReadBytes(bytes, offset, out readSize));

            case MessagePackType.Extension:
                var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
                if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
                {
                    return(MessagePackBinary.ReadDateTime(bytes, offset, out readSize));
                }
                throw new InvalidOperationException("Invalid primitive bytes.");

            case MessagePackType.Array:
            {
                var length      = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                var startOffset = offset;
                offset += readSize;

                var objectFormatter = formatterResolver.GetFormatter <object>();
                var array           = new object[length];
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < length; i++)
                    {
                        array[i] = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                        offset  += readSize;
                    }
                }

                readSize = offset - startOffset;
                return(array);
            }

            case MessagePackType.Map:
            {
                var length      = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
                var startOffset = offset;
                offset += readSize;

                var objectFormatter = formatterResolver.GetFormatter <object>();
                var hash            = new Dictionary <object, object>(length, MessagePackSecurity.Active.GetEqualityComparer <object>());
                using (MessagePackSecurity.DepthStep())
                {
                    for (int i = 0; i < length; i++)
                    {
                        var key = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                        offset += readSize;

                        var value = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
                        offset += readSize;

                        hash.Add(key, value);
                    }
                }

                readSize = offset - startOffset;
                return(hash);
            }

            case MessagePackType.Nil:
                readSize = 1;
                return(null);

            default:
                throw new InvalidOperationException("Invalid primitive bytes.");
            }
        }