UnpackFrom() public method

Deserialize object with specified Unpacker.
public UnpackFrom ( Unpacker unpacker ) : object
unpacker Unpacker which unpacks values of resulting object tree.
return object
Ejemplo n.º 1
0
        public static void UnpackMapTo <TKey, TValue>(Unpacker unpacker, MessagePackSerializer <TKey> keySerializer, MessagePackSerializer <TValue> valueSerializer, IDictionary <TKey, TValue> dictionary)
        {
            if (unpacker == null)
            {
                SerializationExceptions.ThrowArgumentNullException("unpacker");
            }

            if (keySerializer == null)
            {
                SerializationExceptions.ThrowArgumentNullException("keySerializer");
            }

            if (valueSerializer == null)
            {
                SerializationExceptions.ThrowArgumentNullException("valueSerializer");
            }

            if (dictionary == null)
            {
                SerializationExceptions.ThrowArgumentNullException("dictionary");
            }

#if ASSERT
            Contract.Assert(unpacker != null);
            Contract.Assert(keySerializer != null);
            Contract.Assert(valueSerializer != null);
            Contract.Assert(dictionary != null);
#endif // ASSERT

            if (!unpacker.IsMapHeader)
            {
                SerializationExceptions.ThrowIsNotMapHeader(unpacker);
            }

#if ASSERT
            Contract.EndContractBlock();
#endif // ASSERT

            int count = GetItemsCount(unpacker);
            for (int i = 0; i < count; i++)
            {
                if (!unpacker.Read())
                {
                    SerializationExceptions.ThrowMissingItem(i, unpacker);
                }

                TKey key;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    key = keySerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        key = keySerializer.UnpackFrom(subtreeUnpacker);
                    }
                }


                if (!unpacker.Read())
                {
                    SerializationExceptions.ThrowMissingItem(i, unpacker);
                }

                TValue value;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    value = valueSerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        value = valueSerializer.UnpackFrom(subtreeUnpacker);
                    }
                }

                dictionary.Add(key, value);
            }
        }
Ejemplo n.º 2
0
        public static void UnpackMapTo <TKey, TValue>(Unpacker unpacker, MessagePackSerializer keySerializer, MessagePackSerializer valueSerializer, Dictionary <TKey, TValue> dictionary)

        {
#if DEBUG
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            if (!unpacker.IsMapHeader)
            {
                throw SerializationExceptions.NewIsNotMapHeader();
            }

            Contract.EndContractBlock();
#endif

            int count = GetItemsCount(unpacker);
            for (int i = 0; i < count; i++)
            {
                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                TKey key;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    key = (TKey)keySerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        key = (TKey)keySerializer.UnpackFrom(subtreeUnpacker);
                    }
                }


                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                TValue value;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    value = (TValue)valueSerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        value = (TValue)valueSerializer.UnpackFrom(subtreeUnpacker);
                    }
                }

                dictionary.Add(key, value);
            }
        }
Ejemplo n.º 3
0
        public static void UnpackCollectionTo <T, TDiscarded>(Unpacker unpacker, MessagePackSerializer <T> serializer, IEnumerable <T> collection, Func <T, TDiscarded> addition)
        {
            if (unpacker == null)
            {
                SerializationExceptions.ThrowArgumentNullException("unpacker");
            }

            if (serializer == null)
            {
                SerializationExceptions.ThrowArgumentNullException("serializer");
            }

            if (collection == null)
            {
                SerializationExceptions.ThrowArgumentNullException("collection");
            }

            if (addition == null)
            {
                SerializationExceptions.ThrowArgumentNullException("addition");
            }

#if ASSERT
            Contract.Assert(unpacker != null);
            Contract.Assert(serializer != null);
            Contract.Assert(collection != null);
            Contract.Assert(addition != null);
#endif // ASSERT

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

#if ASSERT
            Contract.EndContractBlock();
#endif // ASSERT

            int count = GetItemsCount(unpacker);
            for (int i = 0; i < count; i++)
            {
                if (!unpacker.Read())
                {
                    SerializationExceptions.ThrowMissingItem(i, unpacker);
                }

                T item;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    item = serializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        item = serializer.UnpackFrom(subtreeUnpacker);
                    }
                }

                addition(item);
            }
        }
Ejemplo n.º 4
0
        public static void UnpackMapToDictionary(Unpacker unpacker, MessagePackSerializer keySerializer, MessagePackSerializer valueSerializer, object dictionary)
        {
#if DEBUG
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            if (!unpacker.IsMapHeader)
            {
                throw SerializationExceptions.NewIsNotMapHeader();
            }

            Contract.EndContractBlock();
#endif

            int count = GetItemsCount(unpacker);
            for (int i = 0; i < count; i++)
            {
                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                object key;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    key = keySerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        key = keySerializer.UnpackFrom(subtreeUnpacker);
                    }
                }


                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                object value;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    value = valueSerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        value = valueSerializer.UnpackFrom(subtreeUnpacker);
                    }
                }

                var addMethod = dictionary.GetType().GetMethod("Add", new[] { keySerializer.TargetType, valueSerializer.TargetType });

                addMethod.Invoke(dictionary, new[] { key, value });
            }
        }