protected internal override async Task <T> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var itemsCount = UnpackHelpers.GetItemsCount(unpacker);

            if (itemsCount != this._itemSerializers.Count)
            {
                SerializationExceptions.ThrowTupleCardinarityIsNotMatch(this._itemSerializers.Count, itemsCount, unpacker);
            }

            var unpackedItems = new List <object>(this._itemSerializers.Count);

            for (var i = 0; i < this._itemSerializers.Count; i++)
            {
                if (!await unpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    SerializationExceptions.ThrowMissingItem(i, unpacker);
                }

                unpackedItems.Add(await this._itemSerializers[i].UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false));
            }

            return(this.CreateTuple(unpackedItems));
        }
        protected internal override async Task <T> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var buffer = new TItem[UnpackHelpers.GetItemsCount(unpacker)];

            using (var subTreeUnpacker = unpacker.ReadSubtree())
            {
                // Reverse Order
                for (int i = buffer.Length - 1; i >= 0; i--)
                {
                    if (!await subTreeUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
                    {
                        SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                    }

                    buffer[i] = await this._itemSerializer.UnpackFromAsync(subTreeUnpacker, cancellationToken).ConfigureAwait(false);
                }
            }

            return(Factory(buffer));
        }
        protected internal override T UnpackFromCore(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var itemsCount = UnpackHelpers.GetItemsCount(unpacker);

            if (itemsCount != this._itemSerializers.Count)
            {
                SerializationExceptions.ThrowTupleCardinarityIsNotMatch(this._itemSerializers.Count, itemsCount, unpacker);
            }

            var unpackedItems = new List <object>(this._itemSerializers.Count);

            for (var i = 0; i < this._itemSerializers.Count; i++)
            {
                if (!unpacker.Read())
                {
                    SerializationExceptions.ThrowMissingItem(i, unpacker);
                }

                unpackedItems.Add(this._itemSerializers[i].UnpackFrom(unpacker));
            }

            return(this.CreateTuple(unpackedItems));
        }
        protected internal override T UnpackFromCore(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var buffer = new TItem[UnpackHelpers.GetItemsCount(unpacker)];

            using (var subTreeUnpacker = unpacker.ReadSubtree())
            {
                // Reverse Order
                for (int i = buffer.Length - 1; i >= 0; i--)
                {
                    if (!subTreeUnpacker.Read())
                    {
                        SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                    }

                    buffer[i] = this._itemSerializer.UnpackFrom(subTreeUnpacker);
                }
            }

            return(Factory(buffer));
        }
        protected internal override Stack UnpackFromCore(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            return(new Stack(UnpackItemsInReverseOrder(unpacker, UnpackHelpers.GetItemsCount(unpacker))));
        }
        protected internal override async Task <Stack> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            return(new Stack(await UnpackItemsInReverseOrderAsync(unpacker, UnpackHelpers.GetItemsCount(unpacker), cancellationToken).ConfigureAwait(false)));
        }
Exemple #7
0
        protected internal override void UnpackToCore(Unpacker unpacker, List <MessagePackObject> collection)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            UnpackToCore(unpacker, collection, UnpackHelpers.GetItemsCount(unpacker));
        }
        protected internal override void UnpackToCore(Unpacker unpacker, object collection)
        {
            if (!unpacker.IsMapHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            this.UnpackToCore(unpacker, collection as IDictionary, UnpackHelpers.GetItemsCount(unpacker));
        }
Exemple #9
0
        protected internal override Task UnpackToAsyncCore(Unpacker unpacker, T[] collection, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            return(this.UnpackToAsyncCore(unpacker, collection, UnpackHelpers.GetItemsCount(unpacker), cancellationToken));
        }
Exemple #10
0
        protected internal override Task <TList> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            return(this.InternalUnpackFromAsyncCore(unpacker, cancellationToken));
        }
		protected internal override TDictionary UnpackFromCore( Unpacker unpacker )
		{
			if ( !unpacker.IsMapHeader )
			{
				SerializationExceptions.ThrowIsNotArrayHeader( unpacker );
			}

			return this.InternalUnpackFromCore( unpacker );
		}
Exemple #12
0
        protected internal override TList UnpackFromCore(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            return(this.InternalUnpackFromCore(unpacker));
        }
        protected internal override async Task UnpackToAsyncCore(Unpacker unpacker, Stack collection, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            foreach (var item in await UnpackItemsInReverseOrderAsync(unpacker, UnpackHelpers.GetItemsCount(unpacker), cancellationToken).ConfigureAwait(false))
            {
                collection.Push(item);
            }
        }
        protected internal override void UnpackToCore(Unpacker unpacker, Stack collection)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            foreach (var item in UnpackItemsInReverseOrder(unpacker, UnpackHelpers.GetItemsCount(unpacker)))
            {
                collection.Push(item);
            }
        }
Exemple #15
0
        protected internal override Queue <TItem> UnpackFromCore(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var queue = new Queue <TItem>(UnpackHelpers.GetItemsCount(unpacker));

            this.UnpackToCore(unpacker, queue);

            return(queue);
        }
Exemple #16
0
        protected internal override async Task <Queue <TItem> > UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var queue = new Queue <TItem>(UnpackHelpers.GetItemsCount(unpacker));

            await this.UnpackToAsyncCore(unpacker, queue, cancellationToken).ConfigureAwait(false);

            return(queue);
        }
Exemple #17
0
        protected internal override List <MessagePackObject> UnpackFromCore(Unpacker unpacker)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var count      = UnpackHelpers.GetItemsCount(unpacker);
            var collection = new List <MessagePackObject>(count);

            UnpackToCore(unpacker, collection, count);
            return(collection);
        }
Exemple #18
0
        protected internal override async Task <T[]> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var count  = UnpackHelpers.GetItemsCount(unpacker);
            var result = new T[count];

            await this.UnpackToAsyncCore(unpacker, result, count, cancellationToken).ConfigureAwait(false);

            return(result);
        }
Exemple #19
0
        protected internal override void UnpackToCore(Unpacker unpacker, object collection)
#endif // !UNITY
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

#if !UNITY
            this.UnpackToCore(unpacker, collection, UnpackHelpers.GetItemsCount(unpacker));
#else
            this.UnpackToCore(unpacker, collection as IList, UnpackHelpers.GetItemsCount(unpacker));
#endif // !UNITY
        }
Exemple #20
0
        protected internal override async Task <List <MessagePackObject> > UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var count      = UnpackHelpers.GetItemsCount(unpacker);
            var collection = new List <MessagePackObject>(count);

            await UnpackToAsyncCore(unpacker, collection, count, cancellationToken).ConfigureAwait(false);

            return(collection);
        }
Exemple #21
0
        protected internal override object UnpackFromCore(Unpacker unpacker)
#endif // !UNITY
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var count = UnpackHelpers.GetItemsCount(unpacker);

#if !UNITY
            var result = new T[count];
#else
            var result = Array.CreateInstance(this._itemType, count);
#endif // !UNITY
            this.UnpackToCore(unpacker, result, count);
            return(result);
        }
        protected internal override object UnpackFromCore(Unpacker unpacker)
        {
            if (this._isUnpackable)
            {
                var result = this.CreateInstance(0);
                (( IUnpackable )result).UnpackFromMessage(unpacker);
                return(result);
            }

            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var itemsCount = UnpackHelpers.GetItemsCount(unpacker);
            var collection = this.CreateInstance(itemsCount);

            this.UnpackToCore(unpacker, collection, itemsCount);
            return(collection);
        }
Exemple #23
0
        protected internal override async Task <TCollection> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (this._isAsyncUnpackable)
            {
                var result = this.CreateInstance(0);
                await(( IAsyncUnpackable )result).UnpackFromMessageAsync(unpacker, cancellationToken).ConfigureAwait(false);
                return(result);
            }

            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            var itemsCount = UnpackHelpers.GetItemsCount(unpacker);
            var collection = this.CreateInstance(itemsCount);

            await this.UnpackToAsyncCore(unpacker, collection, itemsCount, cancellationToken).ConfigureAwait(false);

            return(collection);
        }
Exemple #24
0
        private void ReadArrayMetadata(Unpacker metadataUnpacker, out int[] lengths, out int[] lowerBounds)
        {
            if (!metadataUnpacker.Read())
            {
                SerializationExceptions.ThrowUnexpectedEndOfStream(metadataUnpacker);
            }

            if (!metadataUnpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(metadataUnpacker);
            }

            using (var lengthsUnpacker = metadataUnpacker.ReadSubtree())
            {
#if !UNITY
                lengths = this._int32ArraySerializer.UnpackFrom(lengthsUnpacker);
#else
                lengths = this._int32ArraySerializer.UnpackFrom(lengthsUnpacker) as int[];
#endif // !UNITY
            }

            if (!metadataUnpacker.Read())
            {
                SerializationExceptions.ThrowUnexpectedEndOfStream(metadataUnpacker);
            }

            if (!metadataUnpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(metadataUnpacker);
            }

            using (var lowerBoundsUnpacker = metadataUnpacker.ReadSubtree())
            {
#if !UNITY
                lowerBounds = this._int32ArraySerializer.UnpackFrom(lowerBoundsUnpacker);
#else
                lowerBounds = this._int32ArraySerializer.UnpackFrom(lowerBoundsUnpacker) as int[];
#endif // !UNITY
            }
        }
Exemple #25
0
        private async Task <Tuple <int[], int[]> > ReadArrayMetadataAsync(Unpacker metadataUnpacker, CancellationToken cancellationToken)
        {
            if (!await metadataUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
            {
                SerializationExceptions.ThrowUnexpectedEndOfStream(metadataUnpacker);
            }

            if (!metadataUnpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(metadataUnpacker);
            }

            int[] lengths, lowerBounds;

            using (var lengthsUnpacker = metadataUnpacker.ReadSubtree())
            {
                lengths = await this._int32ArraySerializer.UnpackFromAsync(lengthsUnpacker, cancellationToken).ConfigureAwait(false);
            }

            if (!await metadataUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
            {
                SerializationExceptions.ThrowUnexpectedEndOfStream(metadataUnpacker);
            }

            if (!metadataUnpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(metadataUnpacker);
            }

            using (var lowerBoundsUnpacker = metadataUnpacker.ReadSubtree())
            {
                lowerBounds = await this._int32ArraySerializer.UnpackFromAsync(lowerBoundsUnpacker, cancellationToken).ConfigureAwait(false);
            }

            return(Tuple.Create(lengths, lowerBounds));
        }
Exemple #26
0
        protected internal override object UnpackFromCore(Unpacker unpacker)
#endif // !UNITY
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            if (UnpackHelpers.GetItemsCount(unpacker) != 2)
            {
                SerializationExceptions.ThrowSerializationException("Multidimensional array must be encoded as 2 element array.");
            }

            using (var wholeUnpacker = unpacker.ReadSubtree())
            {
                if (!wholeUnpacker.Read())
                {
                    SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                }

                MessagePackExtendedTypeObject metadata;
                try
                {
                    metadata = wholeUnpacker.LastReadData.AsMessagePackExtendedTypeObject();
                }
                catch (InvalidOperationException ex)
                {
                    SerializationExceptions.ThrowSerializationException("Multidimensional array must be encoded as ext type.", ex);
                    metadata = default(MessagePackExtendedTypeObject);                        // never reaches
                }

                if (metadata.TypeCode != this.OwnerContext.ExtTypeCodeMapping[KnownExtTypeName.MultidimensionalArray])
                {
                    SerializationExceptions.ThrowSerializationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            "Multidimensional array must be encoded as ext type 0x{0:X2}.",
                            this.OwnerContext.ExtTypeCodeMapping[KnownExtTypeName.MultidimensionalArray]
                            )
                        );
                }

                int[] lengths, lowerBounds;

                using (var arrayMetadata = new MemoryStream(metadata.Body))
                    using (var metadataUnpacker = Unpacker.Create(arrayMetadata, false))
                    {
                        if (!metadataUnpacker.Read())
                        {
                            SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                        }

                        if (!metadataUnpacker.IsArrayHeader)
                        {
                            SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
                        }

                        if (UnpackHelpers.GetItemsCount(metadataUnpacker) != 2)
                        {
                            SerializationExceptions.ThrowSerializationException("Multidimensional metadata array must be encoded as 2 element array.");
                        }

                        this.ReadArrayMetadata(metadataUnpacker, out lengths, out lowerBounds);
                    }

#if SILVERLIGHT
                // Simulate lowerbounds because Array.Initialize() in Silverlight does not support lowerbounds.
                var inflatedLengths = new int[lengths.Length];
                for (var i = 0; i < lowerBounds.Length; i++)
                {
                    inflatedLengths[i] = lengths[i] + lowerBounds[i];
                }
#endif // SILVERLIGHT
                if (!wholeUnpacker.Read())
                {
                    SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                }

                if (!wholeUnpacker.IsArrayHeader)
                {
                    SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
                }

                using (var arrayUnpacker = wholeUnpacker.ReadSubtree())
                {
                    var result =
                        Array.CreateInstance(
#if !UNITY
                            typeof(TItem),
#else
                            this._itemType,
#endif // !UNITY
#if !SILVERLIGHT
                            lengths,
                            lowerBounds
#else
                            inflatedLengths
#endif // !SILVERLIGHT
                            );

                    var totalLength = UnpackHelpers.GetItemsCount(arrayUnpacker);
                    if (totalLength > 0)
                    {
                        ForEach(
                            result,
                            totalLength,
                            lowerBounds,
                            lengths,
                            indices =>
                        {
                            // ReSharper disable AccessToDisposedClosure
                            if (!arrayUnpacker.Read())
                            {
                                SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                            }

                            result.SetValue(
                                this._itemSerializer.UnpackFrom(arrayUnpacker),
                                indices
                                );
                            // ReSharper restore AccessToDisposedClosure
                        }
                            );
                    }

#if !UNITY
                    return(( TArray )( object )result);
#else
                    return(result);
#endif // !UNITY
                }
            }
        }
Exemple #27
0
        protected internal override async Task <TArray> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
        {
            if (!unpacker.IsArrayHeader)
            {
                SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
            }

            if (UnpackHelpers.GetItemsCount(unpacker) != 2)
            {
                SerializationExceptions.ThrowSerializationException("Multidimensional array must be encoded as 2 element array.");
            }

            using (var wholeUnpacker = unpacker.ReadSubtree())
            {
                if (!await wholeUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                }

                MessagePackExtendedTypeObject metadata;
                try
                {
                    metadata = wholeUnpacker.LastReadData.AsMessagePackExtendedTypeObject();
                }
                catch (InvalidOperationException ex)
                {
                    SerializationExceptions.ThrowSerializationException("Multidimensional array must be encoded as ext type.", ex);
                    metadata = default(MessagePackExtendedTypeObject);                       // never reaches
                }

                if (metadata.TypeCode != this.OwnerContext.ExtTypeCodeMapping[KnownExtTypeName.MultidimensionalArray])
                {
                    SerializationExceptions.ThrowSerializationException(
                        String.Format(
                            CultureInfo.CurrentCulture,
                            "Multidimensional array must be encoded as ext type 0x{0:X2}.",
                            this.OwnerContext.ExtTypeCodeMapping[KnownExtTypeName.MultidimensionalArray]
                            )
                        );
                }

                Tuple <int[], int[]> lengthsAndLowerBounds;

                using (var arrayMetadata = new MemoryStream(metadata.Body))
                    using (var metadataUnpacker = Unpacker.Create(arrayMetadata, false))
                    {
                        if (!metadataUnpacker.Read())
                        {
                            SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                        }

                        if (!metadataUnpacker.IsArrayHeader)
                        {
                            SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
                        }

                        if (UnpackHelpers.GetItemsCount(metadataUnpacker) != 2)
                        {
                            SerializationExceptions.ThrowSerializationException("Multidimensional metadata array must be encoded as 2 element array.");
                        }

                        lengthsAndLowerBounds = await this.ReadArrayMetadataAsync(metadataUnpacker, cancellationToken).ConfigureAwait(false);
                    }

                if (!await wholeUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                }

                if (!wholeUnpacker.IsArrayHeader)
                {
                    SerializationExceptions.ThrowIsNotArrayHeader(unpacker);
                }

                using (var arrayUnpacker = wholeUnpacker.ReadSubtree())
                {
                    var result =
                        Array.CreateInstance(typeof(TItem), lengthsAndLowerBounds.Item1, lengthsAndLowerBounds.Item2);

                    var totalLength = UnpackHelpers.GetItemsCount(arrayUnpacker);
                    if (totalLength > 0)
                    {
                        ForEach(
                            result,
                            totalLength,
                            lengthsAndLowerBounds.Item2,
                            lengthsAndLowerBounds.Item1,
                            async indices =>
                        {
                            // ReSharper disable AccessToDisposedClosure
                            if (!await arrayUnpacker.ReadAsync(cancellationToken).ConfigureAwait(false))
                            {
                                SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                            }

                            result.SetValue(
                                await this._itemSerializer.UnpackFromAsync(arrayUnpacker, cancellationToken).ConfigureAwait(false),
                                indices
                                );
                            // ReSharper restore AccessToDisposedClosure
                        }
                            );
                    }

                    return(( TArray )( object )result);
                }
            }
        }